String.replace
Careful readers may find that we missed the last articleString.replace
This is the way.String.replace
It has more powerful usage and flexibility in JS, so it’s separated here and introduced separately.
API
String.replace(searchValue, replacement)
String.replace
At the same time, support regular expression or string replacement, and return a new string. Because our topic is regular expressions, the following content will focus on regular replacement.
By default,String.replace
Replace only once. If setg
Pattern, all matched strings are replaced
Parameter description
- String: the string to be replaced
- SearchValue: string or regular expression
- Replacement: string or function
usage
String replacement
'I am back end developer'.replace('back','front');
//"I am front end developer"
Directly put theback
replace withfront
. When there are twoback
What’s going on?
'I am back end developer, you are back end developer'.replace('back','front');
//"I am front end developer, you are back end developer"
As you can see, the second oneback
, has not been replaced. If you need to put the otherback
We need to use regular expressions at this time.
Regular expression substitution
Set upg
Pattern, global replacement.
'I am back end developer, you are back end developer'.replace(/back/g,'front');
//"I am front end developer, you are front end developer"
stayreplacement
String, there are some special variables can be used.
Special variables | explain |
---|---|
$1,$2,$3…$n | Represents the text that matches the corresponding capture group |
$& | String matching regular |
$$ | ‘$’ character |
$` | Matches the character to the left of the string |
$’ | Matches the character to the right of the string |
Interesting string replacement
use$&
The string that the operation matches.
Var STR ='interesting string replacement ';
str.replace (/ interesting string /, '($)');
//"(interesting string) substitution"
use$$
statement$
Character.
Var STR ='the price of this commodity is 12.99 ';
str.replace(/\d+\.\d{2}/,'$$$&');
//"The price of this product is $12.99"
Replace content with $’and $’characters
'abc'.replace(/b/,"$`");//aac
'abc'.replace(/b/,"$'");//acc
Use group matching to combine new strings
'2015-05-06'.replace(/(\d{4})-(\d{2})-(\d{2})/,"$3/$2/$1")
//"06/05/2015"
Function parameters
Whenreplacement
When it is a function parameter, the flexibility of string operation will be improved.
explain
'Stirng.replace'.replace(/(\w+)(\.)(\w+)/,function(){
console.log(arguments) // ["Stirng.replace", "Stirng", ".", "replace", 0, "Stirng.replace"]
Return 'the return value will replace the matching character'
})
parameter | explain |
---|---|
match | The string (in this case String.replace ) |
p1, p2, … | Regular uses grouping text when grouping matching, otherwise there is no such parameter (this example is “Stirng”, “.”, “replace”) |
offset | The corresponding index position of the matching string (0 in this example) |
source | The original string (in this case String.replace ) |
Case — Transformation of style attribute
Convert hump character to-
Connector form
'borderTop'.replace(/[A-Z]/g,function(m){
return '-'+ m.toLowerCase()
})
//"border-top"
hold-
Convert connector form to hump form
'border-top'.replace(/-(\w)/g,function(m,s){
return s.toUpperCase()
})
//"borderTop"
The final test
The content given to the reader:
Need toHello-World
Replace with regularHWeolrllod