Detailed description of the string Regular Expression replace method in js, stringreplace
The replace method is a complex method in javascript involving regular expressions. It should be a string object method strictly. It only involves more regular expressions. We need flexible use.
Syntax: stringObj. replace (regexp/substr, replacement );
The first parameter is required. The substring or regular RexExp to be replaced in the string;
The second parameter: required. It is a string value that specifies whether to replace text or generate a function that replaces text.
Return Value: note that the returned value is a new string and does not change the original string. It is obtained after the first match or all matches of regexp are replaced by replacement.
Therefore, it can be divided into many situations based on different parameters. The following is an analysis of various situations:
NO.1 both parameters are strings
Var str1 = 'This is a piece of original text. The content to be replaced "this is to be replaced "! '; Var newStr = str1.replace ('Here to replace', 'need replace '); console. log (newStr); // output: this is a piece of original text, and the content to be replaced is "need replace "!
In the preceding example, the second parameter string 'need replace 'is replaced with the first parameter string', which must be replaced '. This is the simplest form.
No. 2 the first parameter is a regular expression, and the second parameter is a string.
Var str2 = 'This is a piece of original text. The content to be replaced is "ac "! '; Var newStr = str2.replace (/([a-z]) +/g, 'qqq'); console. log (newStr); // output: this is a piece of original text, and the content to be replaced is "qqq, this is to replace qqq "!
The above example string 'qqq' replaces the regular expression Matching content. If regexp has a global flag, the replace () method replaces all matched substrings. Otherwise, it only replaces the first matched substring.
No. 3 the first parameter is a regular expression, and the second parameter is a string with a $ character.
Var str3 = 'This is a piece of original text. "3c, replace 4d "! '; Var newStr = str3.replace (/([0-9]) ([a-z])/g, "$1"); console. log (newStr); // output: this is a piece of original text, "3 this should be replaced by 4 "! ';
In the preceding example, $1 indicates that the first sub-expression in regexp matches a single number ([0-9, similarly, if $2 is used, it indicates the second child ([a-z]). Therefore, the matching '3c 'is replaced by the '3' indicated by the first child, '4d 'is replaced by the first child representing the matched number '4. Others are also available:
(/([0-9]) ([a-z])/g, "$2")-> // output: this is a piece of original text, "c, replace d "! '; (3c and 4d are replaced by the matching c and d by the second child.) (/([0-9]) ([a-z])/g, "$ '")-> // output: this is a piece of original text, "replace d "! Replace "!"! '; (3c is replaced by the text on the right of 3c, and the text on the right of 4d is "! So it appears twice)
No. 4 the first parameter is regular and the second Parameter Function
Var str4 = 'This is a piece of original text. The content to be replaced is "aa". bbb must be replaced with ccccc "! '; Var newStr = str4.replace (/[a-z] +/g, function ($0) {var str = ''; for (var I = 0; I <$0. length; I ++) {str + = '*' ;}; return str ;}); console. log (newStr); // This is a piece of original text, and the content to be replaced is "** this must be replaced *****"!
The first parameter of the above example function is the whole of the matched regexp, and the corresponding text is returned according to the length function;
No. 5 The first parameter is a regular expression with a subexpression. The second parameter function has multiple parameters.
Var str5 = 'This is a piece of original text. The content to be replaced is "3c, this is to replace 4d "! '; Var newStr = str5.replace (/([0-9]) ([a-z])/g, function (arg1, arg2, arg3, arg4, arg5) {console. log (arg1); console. log (arg2); console. log (arg3); console. log (arg4); console. log (arg5 );});
Output:
3c
3
C
17
This is a piece of original text, and the content to be replaced is "3c, this is to replace 4d "!
4d
4
D
23
This is a piece of original text, and the content to be replaced is "3c, this is to replace 4d "!
In the above example, the first parameter arg1 represents the matching whole, arg2 represents the first subexpression, arg3 represents the second subexpression, And the next parameter arg4 is an integer, declares the position where the child match appears in the stringObject. The last parameter is stringObject itself.
The above is the possible situation of the replace method. It is indeed a method that requires in-depth understanding, but it is indeed a very powerful method, worthy of in-depth research!