JS Replace how to implement ReplaceAll

Source: Internet
Author: User

JS under the String object's replace method is defined as follows:

Stringobject.replace (regexp/substr,replacement)

which

Parameters | Describe

Regexp/substr | Necessary. A RegExp object that specifies the substring or pattern to replace.

Replacement | Necessary. A string value. A function that specifies replacement text or generates alternate text.

return value

A new string that is obtained after the first match or all matches of regexp have been replaced with replacement .

Description

The replace () method of the string Stringobject performs a find-and-replace operation. It looks for substrings in Stringobject that match regexp, and then replaces them with replacement . If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matched substring.

replacement can be a string, or it can be a function. If it is a string, then each match is replaced by a string. However, the $ character in replacement has a specific meaning. As shown in the following table, it shows that the resulting string from a pattern match will be used for substitution.

Characters | Replace text

$, $ 、...、 | The text that matches the 1th to 99th sub-expression in RegExp.

$& | A substring that matches the regexp.

$ ' (The one above the TAB key) | The text that is located to the left of the matching substring.

$ ' | The text on the right side of the matching substring.

$$ | Direct volume symbol.

Note: ECMAScript v3 stipulates that the parameter replacement of the replace () method can be a function instead of a string. In this case, each match calls the function, and the string it returns is used as the replacement text. The first parameter of the function is a string that matches the pattern. The next parameter is a string that matches the subexpression in the pattern and can have 0 or more of these parameters. The next argument is an integer that declares where the match appears in the Stringobject. The last parameter is the stringobject itself.

Above is a description of the w3school above for the string object replace method. Let's look at the test:

var str = ' BbBb '; Console.log (str.replace (' B ', ' B '));

At this point the console will print: ' BbBb ' instead of ' bbBb '.

That is, when the argument ' Regexp/substr ' is a string, only the first matched substring is replaced. The JS string object does not provide a ReplaceAll method, so you can only try another method, the parameter ' Regexp/substr ' is a regular Expression object--regexp object.

Let's take a look at what the RegExp object (which has been understood to skip directly), and here's how the RegExp object is created:

Direct Volume Syntax:

/pattern/attributes

Syntax for creating RegExp objects:

New REGEXP (pattern, attributes);
Parameters

Parameter pattern is a string that specifies the pattern of a regular expression or other regular expression.

The parameter attributes is an optional string that contains the property "G", "I", and "M", respectively, for specifying global matching, case-sensitive matching, and multiline matching. The M attribute is not supported until ECMAScript is normalized. If pattern is a regular expression, not a string, you must omit the parameter.

return value

A new RegExp object that has the specified pattern and flags. If the parameter pattern is a regular expression instead of a string, the RegExp () constructor creates a new RegExp object with the same pattern and flags as the specified REGEXP.

Without the new operator, and REGEXP () as a function call, it behaves as if it were called with the new operator, except that when pattern is a regular expression, it only returns pattern, and no longer creates a new The RegExp object.

Thrown

SyntaxError-Throws the exception if pattern is not a valid regular expression, or if the attributes contains characters other than "G", "I", and "M".

TypeError-Throws the exception if pattern is a RegExp object but does not omit the attributes parameter.

Use the RegExp object to test the above example:

var str = ' BbBb '; Console.log (str.replace(new RegExp (' B '), ' B '));

The result is: ' bbBb ', or only the first matched substring is replaced. Let's take a look at the description of the second parameter of the RegExp constructor object:

Modifier | Describe

I | Performs a match that is not case sensitive.

G | Performs a global match (finds all matches rather than stops after the first match is found).

m | Performs multi-line matching.

As you can see from above, when the second argument string contains the character ' G ', a global match is made. Let's Test it here:

var str = ' BbBb '; Console.log (str.replace (New

At this point the print: ' bbbb '. This is the result we want.

Of course, creating regexp objects above can also be used in direct volume.

New REGEXP (' b ')  = =/b/new RegExp (' B ', ' g ') = =/b/g

By the way, look at the case insensitive match, used with the global match:

var str = ' BbBb '; Console.log (Str.replace (/b/gi, ' a '));

This will print: ' AAAA '.

In fact, the key is in the use of regular expressions. Attach the W3school above RegExp object Description: http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp

JS Replace how to implement ReplaceAll

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.