String. Replace ()
Syntax:
String. Replace (Regexp, replacement)
Regexp: the regular expression of the replacement operation. If a string is input, it is processed as a normal character and only one replacement operation is performed. If it is a regular expression, with the global (g) modifier, all target characters will be replaced. Otherwise, only one replacement operation will be performed.
Replacement: The character you want to replace.
The return value is the string after the replacement operation is executed.
String. Replace ()
VaRTEXT = "javascript is very powerful! "; Text. Replace (/JavaScript/I, "JavaScript");//Return: javascript is very powerful!
String. Replace () replace all Target Characters
VaRTEXT = "javascript is very powerful! Javascript is my favorite language! "; Text. Replace (/JavaScript/ig, "JavaScript");//Return: javascript is very powerful! Javascript is my favorite language!
String. Replace ()
VaRName = "doe, John"; Name. Replace (/(\ W +) \ s *, \ s * (\ W +)/, "$2 $1");//Return Value: John Doe
String. Replace () is used to replace all characters in double quotation marks with characters in brackets.
VaRTEXT = '"JavaScript" is very powerful! '; Text. Replace (/"([^"] *) "/G," [$1]");//Response: [JavaScript] is very powerful!
String. Replace () uppercase letters
VaRTEXT = 'a journey of a thousand miles begins with single step .'; Text. Replace (/\ B \ W + \ B/g,Function(Word ){ReturnWord. substring (0, 1). touppercase () +Word. substring (1);});//Return Value: A journey of a thousand miles begins with single step.
From above: http://www.codebit.cn/javascript/javascript-replace.html
String. Replace () parameter description
When the first parameter is a regular expression and the second parameter is a function, $ in the function parameter of the second parameter indicates the regular expression matching string, $1, $2... group Content:
"ABC {name} test". Replace (/{(.*?)} /IMG, function ($, $1 ) {console. log ($, $ 1); /// output {name} name return $1 ;});