Home>

In JavaScript, the replace method is a string object and can be used to replace a string.Today we will discuss in detail the use of some replace () methods.

About definition

The replace () method is used to replace some characters in a string with others,Or replace a substring that matches the regular expression.

About syntax

stringobject.replace (regexp/substr, replacement)

About parameters

parameter description

regexp/substr

essential. A regexp object specifying the substring or pattern to be replaced.

Note that if the value is a string,Then use it as a direct text pattern to retrieve,Rather than being converted to a regexp object first.

replacement essential. A string value.Specifies a function for generating or replacing text.

1. The first parameter regexp/substr, using the global identifier g when using regular expressions, can replace all matching substrings,Otherwise it will match only once.

E.g:

var str="aaaaa";
var str1=str.replace ("a", "b");
var str2=str.replace (/a/g, "b");

Running result:str1->"baaaa", str2->"bbbbb"

2. The second parameter, replacement, can be a string or a function, or it can have the specific meaning of the $character.

character Replace text
$1, $2, ..., $99 Text that matches the 1st to 99th subexpressions in regexp.
$& Substring that matches regexp.
$` The text to the left of the matching substring.
$" The text to the right of the matching substring.
$$ Direct quantity symbol.

a. Function example:

var str="111222aa";
str.replace (/(\ d {3})/g, function (word) {
 return "b";
});

Running result:"bbaa"

b. Example of $

var str="1234567890";
str.replace (/(\ d {3}) (\ d {3}) (\ d {4})/g, "$1- $2- $3");

Running result:"123-456-7890"

Note:Most of the time, the combination of regexp and $is used to achieve the demand.So it is necessary to know some basic regular expression rules.

String replacement

"i am loser!". replace ("loser", "hero")

//i am hero!

Using strings directly can change you from lose to hero, but if there are two losers, you cannot become hero together.

"i am loser, you are loser" .replace ("loser", "hero");
//i am hero, you are loser

Replace regular expression with string

"i am loser, you are loser" .replace (/loser/g, "hero")
//i am hero, you are hero

Using regular expressions,And change the regular global property to true to make all losers become hero

Funny replacement characters

replacevalue can be a string.If there are several specific characters in the string,Will be converted to a specific string.

Character replacement text

$&A string that matches the regular

$`Matches the character to the left of the string

$"Matches the character to the right of the string

$1, $2, $, 3,…, $n Matching results in the corresponding group

Bracket matching characters with $&characters

var sstr="Discuss the use of replace in regular expressions";

sstr.replace (/regular expression /, "{$&}");

//Discuss the use of replace in {regular expressions}

Replace content with $`and $" characters

"abc" .replace (/b /, "$` ");

//aac

"abc" .replace (/b /, "$" ");

//acc

Combine new strings using group matching

//@nimojs

replacevalue parameter can be a function

replacevalue in stringobject.replace (searchvalue, replacevalue) can be a function.

If replacevalue is a function thenThe arguments of this function will have n + 3 parameters (n is the number of regular matches)

First look at examples to help understand:

function logarguments () {
 console.log (arguments);
//["[email protected] "," nimojs ","@"," 126.com ", 0," [email protected] "]
 return "The return value will replace the matched target"
}
console.log (
 "[email protected]" .replace (/(.+)(@)(.*)/, logarguments)
)

The parameters are

Multiple if group matching is used for regex, otherwise this parameter is not available.(The parameters of this example are "nimojs", "@", "126.com". It is recommended to modify the regularity to/nimo/to see the arguments value returned in the console)

The corresponding index position of the matched string (0 in this example)

Use a custom function to change the a-g string to lowercase

"javascript" .replace (/[a-g]/g, function () {
 return arguments [0] .tolowercase ();
})
//javascript

Remove callbacks from inline styles using custom functions for callback substitution

"&SpanMicrosoft Yahei"
 var sresult=arguments [0];
 console.log (sresult);//"Microsoft Yahei"
 sresult=sresult.replace (/\ "/g," ");
 console.log (sresult);
//Microsoft Yahei
 return sresult;
})
//<span>demo</span>
  • Previous Summary of the causes of session invalidation and how to solve it
  • Next PHP RMB amount to upper case example code