The Replace method is a string object and can be used to replace strings.
Brief introduction:
Stringobject.replace (Searchvalue,replacevalue)
- Stringobject: String
- Searchvalue: string or Regular expression
- Replacevalue: String or function
String substitution string
JavaScript
1 |
' I am loser! ' . Replace(' loser ',' hero ')//i am hero! |
The use of strings directly allows you to change from loser to hero, but if you have 2 loser you can't turn hero together.
JavaScript
1 |
' I am loser,you is loser '. Replace(' loser ',' hero '); I am Hero,you is loser |
The regular expression is replaced with a string
JavaScript
1 |
' I am loser,you is loser '. Replace(/loser/g,' hero ')//i am hero,you are hero |
Use regular expressions and change the regular global property to True to make all loser become hero
Interesting substitution characters
Replacevalue can be a string. If there are several specific characters in the string, it is converted to a specific string.
Character |
Replace text |
$& |
A string that matches a regular match |
$
|
Match the character to the left of the string |
$ |
Match the character to the right of the string |
$1,$2,$,3,..., $n |
Matching results for the corresponding grouped matches |
Use $& characters to add parentheses to matching characters
JavaScript
12 |
var sStr=' discusses the use of Replace in regular expressions '; sStr. Replace(/Regular expression /,' {$&} '); Discuss the use of Replace in {regular expression} |
Replace content with $ and $ ' characters
JavaScript
12 |
' abc '. Replace(/b/,"$ '"); AAC' abc '. Replace(/b/,"$ '"); ACC |
Combine new strings with grouping matches
JavaScript
1 |
' [email protected] '. Replace(/(. +)(@)(. *)/,"$2$1")//@nimojs |
The Replacevalue parameter can be a function
The replacevalue in Stringobject.replace (Searchvalue,replacevalue) can be a function.
If Replacevalue is a function, then the arguments of this function will have n+3 parameters (the number of times that n matches the regular match)
First look at examples to help understand:
JavaScript
1234567 |
function logarguments(){ console. Log(arguments); ["[Email protected]", "Nimojs", "@", "126.com", 0, "[email protected]"] return ' returned value will replace match to target ' }console. Log( ' [email protected] '. Replace(/(. +)(@)(. *)/,logarguments) ) |
Parameters are
- Match to the string (this example is [email protected], it is recommended to modify the above code of the regular to see the matching characters to help understand)
- If the regular uses a grouping match for more than one otherwise there is no this parameter. (The parameters for this example are "Nimojs", "@", "126.com", respectively.) Recommended modification Regular is/nimo/view the arguments value returned in console)
- Match the corresponding index position of the string (this example is 0)
- Raw string (This example is [email protected])
Use a custom function to change the a-g string to lowercase
JavaScript
123 |
' JAVASCRIPT ' . Replace/[a-g]/gfunction ({ return arguments[ 0]. Tolowercase }) //javascript |
Use a custom function to make a callback replace the single quotation mark in the inline style
JavaScript
1234567 |
' <span style= ' font-family:\ ' Microsoft Ya Black '; >demo</span> '. Replace(/\ ' [^ ']+\ '/g,function(){ var sresult=arguments[0]; console. Log(sresult); ' Microsoft Ya Black ' Sresult=sresult. Replace(/\ '/g,'); console. Log(sresult); Microsoft Ya-Black return sresult; })//<span style= "font-family: Microsoft Jacob Black;" >demo</span> |
The last little trial sledgehammer
This section is for the reader to play the content:
1. Using functions to implement substitution characters autonomously
Character |
Replace text |
$& |
A string that matches a regular match |
$` |
Match the character to the left of the string |
$ |
Match the character to the right of the string |
Use regular but not using the substitution character method above to implement three instances of interesting substitution characters .
2. Wash poker
Need to replace thisnimojs-javascript with a T-JhaivsaNsicmr oiJpst
Original link: http://www.nimojs.com/blog/javascript-replace/
In-depth understanding of the Replace method in JavaScript (go)