The character substitution function in JS string.replace () use Technique _javascript skill

Source: Internet
Author: User

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) parameter description
Regexp/substr required. 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 required. 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 replacement text
$ 、...、 $99 The text that matches the 1th to 99th subexpression in the RegExp.
$& the substring that matches the regexp.
$ ' is the text on the left side of the matching substring.
$ ' is the text on the right side of the matching substring.
$$ Direct measure symbol.

Note: The ECMAScript v3 stipulates that the parameter replacement of the replace () method can be a function rather than 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 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 "jb51.net" to replace "Microsoft" in the string:

<script type= "Text/javascript" >
var str= "Visit microsoft!"
document.write (Str.replace (/microsoft/, "jb51.net")
</script>

Output:

Visit jb51.net!

Example 2
in this case, we will perform a global substitution, which is replaced with "jb51.net" 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, "jb51.net")
</script>

Output:

Welcome to jb51.net! We are proud to announce that Jb51.net
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 matching string uppercase characters are correct:

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

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+)/, "$ $");

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);}
 );

String.Replace () Introduction
Grammar:
var strings = String.Replace (regexp, replacement)

RegExp: The regular expression for which you want to perform the substitution operation. If a string is passed in, it is treated as a normal character, and only one substitution operation is performed, and if the regular expression is present with the global (g) modifier, all occurrences of the target character are replaced. Only one substitution operation will be performed.
Replacement: The character you want to replace.
The return value is the string after the substitution operation.


Simple usage of one string.replace ()
var text = "JavaScript is very powerful!" Text.replace (/javascript/i, "JavaScript"); 14//back: JavaScript is very powerful!

String.Replace () Replaces all occurrences of the target character
var text= "JavaScript is very powerful! JAVASCRIPT is one of my favorite languages! Text.replace (/javascript/ig, "JavaScript"); 18//back: JavaScript is very powerful! JavaScript is one of my favorite languages!

String.Replace () to implement the swap position

var name= "Doe, John";
Name.replace (/(\w+) \s*,\s* (\w+)/, "$ $");
Back: John Doe

String.Replace () implementation replaces all double quotes with characters enclosed in brackets
The var text = ' JavaScript ' is very powerful! ' Text.replace ([^ "]*)"/g, "[$]"); 26//return: [JavaScript] very powerful!

String.Replace () capitalize all characters first letter
var text = ' A journey of a thousand miles begins with single step. ' Text.replace (/\b\w+\b/g, function (word) {return word.substring (0,1). toUpperCase () +31 word.substring (1); 32}); 33 34//Return: A Journey of a thousand Miles begins with single step.

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.