Explanation of the regular expression replace example of the string object method in js

Source: Internet
Author: User
This article mainly introduces the regular expression replace example of the string object method in js. The replace method is a complicated method in javascript involving regular expressions, strictly speaking, it should be a string object method. This article describes how to replace the regular expression string in javascript. The replace method is a complicated method in javascript involving regular expressions. It should be a string object method strictly.

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 "! ';

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!

The above is the detailed description of the regular expression replace example of the string object method in js. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.