This JavaScript tutorial explains how to use the string approach called replace() with syntax and examples.
Description
In JavaScript, replace() is a string method that is used to change occurrences of a unique string or regular expression with a replacement string. Because the replace() approach is a technique of the String object, it should be invoked thru a precise occasion of the String class.
Syntax
In JavaScript, the syntax for the replace() method is:
string.replace(search_expression, replacement);
Parameters or Arguments
search_expression It is both a string value or a RegExp object that will be searched for in string. As a RegExp object, it can be a aggregate of the following: Value Description ^ Matches the opening of a string. If used with a match_parameter of ‘m’, it suits the start of a line anywhere within expression. $ Matches the quit of a string. If used with a match_parameter of ‘m’, it suits the give up of a line somewhere within expression. * Matches zero or more occurrences. + Matches one or greater occurrences. ? Matches zero or one occurrence. . Matches any character without NULL. | Used like an “OR” to specify extra than one alternative. [ ] Used to specify a matching list the place you are trying to fit any one of the characters in the list. [^ ] Used to specify a nonmatching listing the place you are trying to suit any character except for the ones in the list. ( ) Used to group expressions as a subexpression. \b Matches a word boundary \B Matches a non-word boundary {m} Matches m times. {m,} Matches at least m times. {m,n} Matches at least m times, but no extra than n times. \n n is a quantity between 1 and 9. Matches the nth subexpression discovered within ( ) earlier than encountering \n. [..] Matches one collation aspect that can be more than one character. [::] Matches personality classes. [==] Matches equivalence classes. \d Matches a digit character. \D Matches a nondigit character. \w Matches a word character. \W Matches a nonword character. \s Matches a whitespace character. \S matches a non-whitespace character. \t fits a horizontal tab character. \v suits a vertical tab character. \r fits a carriage return character. \f suits a shape feed character. \n fits a line feed character. [\b] matches a backspace character. \0 suits a NUL character. *? Matches the previous sample zero or extra occurrences. +? Matches the previous sample one or extra occurrences. ?? Matches the previous sample zero or one occurrence. {n}? Matches the previous pattern n times. {n,}? Matches the preceding pattern at least n times. {n,m}? Matches the preceding sample at least n times, but no longer more than m times. replacement It is the replacement string or a feature (that returns a alternative string). The $ persona is a distinctive character that is used in the substitute string to point out that the price from the pattern healthy can be used as phase of the replacement string. Here are the methods that you can use the $ character: Value Description $1, $2 … $n Used to insert the substring from a parenthesized match. $1 is the 1st parenthesized match, $2 is the 2nd parenthesized match, … $n is the last parenthesized match $& Used to insert the matched substring \\$ Used to insert the literal $ character $+ Used to insert the substring from the closing parenthesized match $’ Used to insert the string to the right of the matched substring $` Used to insert the string to the left of the matched substring
Returns
The replace() approach returns a string with either the first healthy or all fits of search_expression replaced with substitute (see example below as the replacement behavior modifications depending on the type of search_expression).
Note
The replace() method does not trade the value of the unique string. The replace() method will return different effects depending on whether or not the g attribute used to be specified. See the example below for an explanation.
Example
Let’s take a look at an example of how to use the replace() method in JavaScript.
String as the Search Expression
The simplest way to use the replace() method is to find and change one string with every other string. This method does not involve regular expression objects.
For example:
var totn_string = 'TechOnTheNet is great';
console.log(totn_string.replace('great', 'the best'));
In this example, we have declared a variable referred to as totn_string that is assigned the string value of ‘TechOnTheNet is great’. We have then invoked the replace() approach of the totn_string to locate and substitute the first occurrence or match.
We have written the output of the replace() method to the web browser console log, for demonstration purposes, to exhibit what the replace() approach returns.
The following will be output to the web browser console log:
TechOnTheNet is the best
In this example, the replace() approach performs a search for the first prevalence of the string ‘great’ and replaces it with the string ‘the best’. As you can see, the replace() technique again the string ‘TechOnTheNet is the best’.
Regular Expression as the Search Expression (Single Match)
Next, we’ll take a look at everyday expressions. You can use the replace() method to search for and substitute the first prevalence (ie: single match) of a ordinary expression pattern.
For example:
var totn_string = 'We Want to Replace the First Uppercase Character';
console.log(totn_string.replace(/[A-Z]/, 'Y'));
The following will be output to the internet browser console log:
Ye Want to Replace the First Uppercase Character
In this example, the replace() approach performs a search for the first occurrence of an uppercase persona and replaces that persona with ‘Y’. The replace() method returns the new string ‘Ye Want to Replace the First Uppercase Character’ where the ‘W’ used to be replaced with ‘Y’.
Regular Expression as the Search Expression (Global Match)
You can also use the replace() approach to search for all fits of a normal expression pattern. This is done by using performing a global in shape as distinctive by means of the g attribute.
For example:
var totn_string = 'We Want to Replace all Uppercase Characters';
console.log(totn_string.replace(/[A-Z]/g, 'Y'));
The following will be output to the web browser console log:
Ye Yant to Yeplace all Yppercase Yharacters
In this example, the replace() method carried out a world match and searched for all occurrences of uppercase characters and replaced them with ‘Y’. This used to be carried out by way of specifying the g attribute at the cease of the normal expression.
This time the replace() lower back the new string ‘Ye Yant to Yeplace all Yppercase Yharacters’.
Leave a Review