In-depth understanding of the Replace method in JavaScript (GO)

Source: Internet
Author: User

The Replace method is a string object and can be used to replace strings.

Brief introduction:

Stringobject.replace (Searchvalue,replacevalue)

    1. Stringobject: String
    2. Searchvalue: string or Regular expression
    3. Replacevalue: String or function
String substitution string

JavaScript
1 ' I am loser! ' . Replace(' loser ',' hero ')//i am hero!

The use of strings directly allows you to change from loser to hero, but if you have 2 loser you can't turn hero together.

JavaScript
1 ' I am loser,you is loser '. Replace(' loser ',' hero '); I am Hero,you is loser

The regular expression is replaced with a string

JavaScript
1 ' I am loser,you is loser '. Replace(/loser/g,' hero ')//i am hero,you are hero

Use regular expressions and change the regular global property to True to make all loser become hero

Interesting substitution characters

Replacevalue can be a string. If there are several specific characters in the string, it is converted to a specific string.

Character Replace text
$& A string that matches a regular match
$ Match the character to the left of the string
$ Match the character to the right of the string
$1,$2,$,3,..., $n Matching results for the corresponding grouped matches
Use $& characters to add parentheses to matching characters

JavaScript
12 var sStr=' discusses the use of Replace in regular expressions '; sStr. Replace(/Regular expression /,' {$&} '); Discuss the use of Replace in {regular expression}

Replace content with $ and $ ' characters

JavaScript
12 ' abc '. Replace(/b/,"$ '"); AAC' abc '. Replace(/b/,"$ '"); ACC

Combine new strings with grouping matches

JavaScript
1 ' [email protected] '. Replace(/(. +)(@)(. *)/,"$2$1")//@nimojs

The Replacevalue parameter can be a function

The replacevalue in Stringobject.replace (Searchvalue,replacevalue) can be a function.
If Replacevalue is a function, then the arguments of this function will have n+3 parameters (the number of times that n matches the regular match)
First look at examples to help understand:

JavaScript
1234567 function logarguments(){ console. Log(arguments); ["[Email protected]", "Nimojs", "@", "126.com", 0, "[email protected]"] return ' returned value will replace match to target ' }console. Log( ' [email protected] '. Replace(/(. +)(@)(. *)/,logarguments) )

Parameters are

    1. Match to the string (this example is [email protected], it is recommended to modify the above code of the regular to see the matching characters to help understand)
    2. If the regular uses a grouping match for more than one otherwise there is no this parameter. (The parameters for this example are "Nimojs", "@", "126.com", respectively.) Recommended modification Regular is/nimo/view the arguments value returned in console)
    3. Match the corresponding index position of the string (this example is 0)
    4. Raw string (This example is [email protected])
Use a custom function to change the a-g string to lowercase

JavaScript
123 ' JAVASCRIPT ' . Replace/[a-g]/gfunction ({      return arguments[ 0]. Tolowercase }) //javascript

Use a custom function to make a callback replace the single quotation mark in the inline style

JavaScript
1234567 ' <span style= ' font-family:\ ' Microsoft Ya Black '; >demo</span> '. Replace(/\ ' [^ ']+\ '/g,function(){ var sresult=arguments[0]; console. Log(sresult); ' Microsoft Ya Black ' Sresult=sresult. Replace(/\ '/g,'); console. Log(sresult); Microsoft Ya-Black return sresult; })//<span style= "font-family: Microsoft Jacob Black;" >demo</span>

The last little trial sledgehammer

This section is for the reader to play the content:

1. Using functions to implement substitution characters autonomously
Character Replace text
$& A string that matches a regular match
$` Match the character to the left of the string
$ Match the character to the right of the string

Use regular but not using the substitution character method above to implement three instances of interesting substitution characters .

2. Wash poker

Need to replace thisnimojs-javascript with a T-JhaivsaNsicmr oiJpst

Original link: http://www.nimojs.com/blog/javascript-replace/

In-depth understanding of the Replace method in JavaScript (go)

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.