JavaScript Replace () method + Character Subset Introduction (*)

Source: Internet
Author: User

Key two parts of the knowledge point

1. JavaScript replace () function usage

The following replace usage is reproduced from w3cschool:http://www.w3school.com.cn/jsref/jsref_replace.asp

Definitions and Usage

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

Stringobject.replace (regexp/substr,replacement)
Parameters Description
Regexp/substr

Necessary. The RegExp object that prescribes the substring or pattern to be replaced.

Note that if the value is a string, it is used as the direct text pattern to retrieve, not first converted to the RegExp object.

Replacement Necessary. A string value. Provides a function that replaces text or generates alternate text.
return value

A new string that is replaced with replacement for the first match of RegExp or after all matches have been made. Description

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

Replacement can be either a string or 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 string that is matched from the pattern will be used for substitution.

character Replace text
$, $ 、...、 $99 Text that matches the 1th to 99th subexpression in RegExp.
$& The substring that matches the regexp.
$` The text located on the left side of the matching substring.
$' The text that is located to the right of the matching substring.
$$ The direct measure symbol.

Note: The ECMAScript v3 stipulates that the parameter replacement of the replace () method can also be a function. 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 argument 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 Example 1

In this case, we'll use "w3school" to replace "Microsoft" in the string:

<script type= "Text/javascript" >

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

</script>

Output:

Visit w3school!
Example 2

In this case, we will perform a global substitution, which is replaced with "W3school" whenever "Microsoft" is found:

<script type= "Text/javascript" >

var str= "Welcome to microsoft! "
Str=str +" We are proud to announce this is Microsoft has "
Str=str +" one of the largest WEB developers sites in t He world. "

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

</script>

Output:

Welcome to w3school! We are proud to announce this w3school has one of the
largest WEB developers in the world.
Example 3

You can use the code provided in this example to ensure that matching string uppercase characters are correct:

Text = "JavaScript Tutorial";
Text.replace (/javascript/i, "JavaScript");

Output:
JavaScript Tutorial

Example 4

In this case, we'll convert "Doe, John," to "John Doe" in the form of:

Name = "Doe, John";
Name.replace (/(\w+) \s*, \s* (\w+)/, "$ $"); 
Two-word expression:
(\w+) \s*, \s* (\w+)

Example 5

In this case, we'll replace all the curly quotes with straight quotes:

Name = ' "A", "B";
Name.replace ([^ "]*)"/g, "' $ '");
Example 6

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

Name = ' AAA bbb CCC ';
Uw=name.replace (/\b\w+\b/g, function (word) {return
  word.substring (0,1). toUpperCase () +word.substring (1);}
  );


^: matches the starting position of the input string, unless used in a bracket expression, at which point it means that the character set is not accepted. \b: Matches a word boundary, which refers to the position between the word and the space.

2. Description of the use of character subset in regular expressions
You should have a correct understanding of a subset of characters when you define regular expressions
Character Subset

A subset of the regular expression string Matching strings Combination mode
[ABC] Character A,b or C, including CJK EXTB region kanji Simple subset
[^ABC] Any character that is not a,b or C. Exclude
[A-za-z] From A to Z, or A to Z, contains a,z,a,z. Interval (subset)
[A-d[m-p]] From A to D, or M to P, equals [a-dm-p]. Joint
[A-z&&[def]] D,e or F. Intersection
[A-Z&&[^BC]] From A to Z, except B and C, equal to [ad-z] Deduction
[A-z&&[^m-p]] From A to Z, and not including from m to p, equal to [a-lq-z] Deduction


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.