The substitution of strings in JS

Source: Internet
Author: User

Definition and usage

The replace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression.

Grammar

Stringobject.replace (regexp/substr,replacement) parameter description
Regexp/substr required. A RegExp object that specifies the substring or pattern to replace.

Note that if the value is a string, it is used as the direct volume text pattern to be retrieved, instead of being converted to the RegExp object first.

Replacement required. 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.

Character substitution text
$, $ 、...、 The text that matches the 1th to 99th sub-expression in RegExp.
$& the substring that matches the regexp.
$ ' text that is located to the left of the matching substring.
$ ' 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.
Instance

I, replace method

The function of this method is to replace all the specified characters in the string and then generate a new string. After the method call, the original string does not change. For example:

String s = "Abcat";

String S1 = s.replace (' A ', ' 1 ');

The purpose of this code is to replace all character A in string s with character 1, and the resulting new string S1 value is "1bc1t", while the contents of the string s do not change.

If you need to replace a specified string in a string with a different string, you can use the ReplaceAll method, for example:

String s = "Abatbac";

String S1 = s.replaceall ("ba", "12");

The purpose of this code is to replace all the string "AB" in the string s with "12", to generate a new string "a12t12c", and the contents of the string s will not change.

If you only need to replace the first occurrence of the specified string, you can use the Replacefirst method, for example:

String s = "Abatbac";

String S1 = S. Replacefirst ("Ba", "12");

The purpose of this code is to replace only the string "," which appears globally in the string s, with the string "/".

var str = "Is,is,the,cost,of,of,gasoline,going,up,up";

document.write (Str.replace (/\,/g, '/'));

Use the regular global match to replace the global "," with "/".

Example 1
In this example, we will replace "Microsoft" in the string with "jb51.net":

?
1234 <script type="text/javascript">varstr="Visit Microsoft!"document.write(str.replace(/Microsoft/, "jb51.net"))</script>

Output:

Visit jb51.net!

Example 2
In this example, we will perform a global substitution, and whenever "Microsoft" is found, it is replaced with "jb51.net":

?
123456 <script type="text/javascript">varstr="Welcome to Microsoft! "str=str + "We are proud to announce that Microsoft has "str=str + "one of the largest Web Developers sites in the world."document.write(str.replace(/Microsoft/g, "jb51.net"))</script>

Output:

Welcome to jb51.net! We is proud to announce that Jb51.net
Have one of the largest WEB developers sites in the world.

Example 3
You can use the code provided in this example to ensure that the matching string is correct for uppercase characters:

?
12 text = "javascript Tutorial";text.replace(/javascript/i, "JavaScript");

Example 4
In this example, we will convert "Doe, John" to the form of "John Doe":

?
12 name = "Doe, John";name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1");

Example 5
In this example, we'll replace all the curly quotes with straight quotes:

?
12 name = ‘"a", "b"‘;name.replace(/"([^"]*)"/g, "‘$1‘");

Example 6
In this example, we will convert all of the words in the string to uppercase in the first letter:

?
1234 name = ‘aaa bbb ccc‘;uw=name.replace(/\b\w+\b/g, function(word){ returnword.substring(0,1).toUpperCase()+word.substring(1);} );

The substitution of strings in JS

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.