JS replaces the specified character with regular mate replace

Source: Internet
Author: User

There are many ways to replace the specified characters, in this article for everyone in detail, JS using regular with replace is how to do, like friends can refer to the next

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,replacement)

Parameter description
RegExp required. A RegExp object that specifies the 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 no match will be replaced by the 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.

Example

Example 1

In this example, we will replace "Microsoft" in the string with "W3school":

Copy CodeThe code is as follows:
<script type= "Text/javascript" >

var str= "Visit microsoft!"
document.write (Str.replace (/microsoft/, "W3school"))

</script>
Output:

Visit w3school!

Example 2

In this example, we will perform a global substitution, and whenever "Microsoft" is found, it is replaced with "W3school":
Copy CodeThe code is as follows:
<script type= "Text/javascript" >

var str= "Welcome to microsoft! "
Str=str + "We are proud to announce that Microsoft have"
Str=str + "One of the largest WEB developers sites in the world."

document.write (Str.replace (/microsoft/g, "W3school"))

</script>
Output:

Welcome to w3school! We is proud to announce that W3school
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:
Copy CodeThe code is as follows:
Text = "JavaScript Tutorial";
Text.replace (/javascript/i, "JavaScript");
Example 4

In this example, we will convert "Doe, John" to the form of "John Doe":
Copy CodeThe code is as follows:
Name = "Doe, John";
Name.replace (/(\w+) \s*, \s* (\w+)/, "$ $");
Example 5

In this example, we'll replace all the curly quotes with straight quotes:
Copy CodeThe code is as follows:
name = ' A ', ' B ';
Name.replace (/"([^"]*) "/g," ' $ ' ");
Example 6

In this example, we will convert all of the words in the string to uppercase in the first letter:
Copy CodeThe code is as follows:
Name = ' AAA bbb CCC ';
Uw=name.replace (/\b\w+\b/g, function (word) {
Return word.substring (0,1). toUpperCase () +word.substring (1);}
);
Example 7
Copy CodeThe code is as follows:
var str= "FSAF$A$ASSDFDASFA$A$DSFADSF";
var strr= ' \$ ' + ' a ' + ' \$ ';
var name = ' A ', ' B ';
var reger=new RegExp ("[\$]a[\$]", "GM");

Alert (str.replace (Reger, ' 555888 '));

JS replaces the specified character with regular mate replace

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.