JS uses regular expressions and replace to replace specified characters

Source: Internet
Author: User

Definition and usage

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

Syntax

StringObject. replace (regexp, replacement)

Parameter description
Regexp is required. Specifies the RegExp object of the pattern to be replaced. Note that if the value is a string, it is used as the direct text mode to be retrieved, rather than being converted to a RegExp object first.
Replacement is required. A string value. Specifies the function for replacing text or generating replacement text.

Return Value

A new string is obtained after the first match of regexp or all matches are replaced by replacement.

Description

The replace () method of the string stringObject performs the search and replace operation. It searches stringObject for substrings that match regexp and replaces them with replacement. If regexp has a global flag, the replace () method replaces all matched substrings. Otherwise, it only replaces the first matched substring.

Replacement can be a string or a function. If it is a string, no matching will be replaced by the string. However, the $ character in replacement has a specific meaning. As shown in the following table, it indicates that the string obtained from pattern matching will be used for replacement.
Character replacement text
$1, $2,..., and $99 match the text of the 1st to 99th subexpressions in regexp.
$ & A substring that matches regexp.
$ 'Text on the left of the matched substring.
$ 'Text on the right of the matched substring.
% Direct Volume symbol.

Note: ECMAScript v3 stipulates that the replacement parameter of the replace () method can be a function rather than a string. In this case, each match calls this function, and the string it returns will be used as the replacement text. The first parameter of this function is a matching string. The following parameter is a string that matches the subexpression in the pattern. There can be 0 or more such parameters. The following parameter is an integer that declares the position where the matching occurs in the stringObject. The last parameter is stringObject itself.

Instance

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 replacement. Whenever "Microsoft" is found, it will be 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 has"
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 are proud to announce that W3School
Has 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 uppercase characters of the matching string are correct:
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 "John Doe:
Copy codeThe Code is as follows:
Name = "Doe, John ";
Name. replace (/(\ w +) \ s *, \ s * (\ w +)/, "$2 $1 ");

Example 5

In this example, we will replace all the quotation marks with straight quotation marks:
Copy codeThe Code is as follows:
Name = '"a", "B "';
Name. replace (/"([^"] *) "/g," '$1 '");

Example 6

In this example, the first letter of all words in the string is converted to uppercase:
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, '123 '));

Related Article

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.