Weird issues related to js replace regular expressions

Source: Internet
Author: User

 
Test nickname: jesse $ <

View code: Javascript code
Copy codeThe Code is as follows:
$ Id ("f_msg_grow_1 "). innerHTML = msg ["not_club"] ["grow_value"]. replace (// {NICK_NAME}/, this.info ["friend"] ["nick_name"]);

The code will soon notice two points: "replace regular" and "$ ".

In js, replace supports regular expressions, and $ is exactly a special character for regular expression backward matching, which is easy to think of as a problem caused by regular expressions. As far as I know, regular expressions reference only $1, $2, $3, $4 ...... There is only one "$" with no number.

The nickname "jesse $ <" is processed by special html characters in the background and uploaded to the front-end: "jesse $ <", check the page results and note that "&" after "$" is also replaced. Is "$ &" in js indicating full matching of regular expressions and backward references?

Then, the regular expression is removed and the code is changed to Javascript code.
Copy codeThe Code is as follows:
$ Id ("f_msg_grow_1 "). innerHTML = msg ["not_club"] ["grow_value"]. replace ("{NICK_NAME}", this.info ["friend"] ["nick_name"]);

The results are the same, so weird !!!

What's more strange is that I checked it under IE and it was correctly displayed (I 've been testing it under ff before )!

Test code: Javascript code
Copy codeThe Code is as follows:
Document. write ("NAME: {NAME}". replace (/\ {NAME}/g, "$ &"));

IE and ff show the same: C-sharp code
NAME: Zhang Jianguang {NAME} amp;
After testing, "$ &" really indicates the full matching of regular expressions and backward references, so I am very lonely. I didn't know it before!

Test code: Javascript code
Copy codeThe Code is as follows:
Document. write ("NAME: {NAME}". replace ("{NAME}", "Zhang Jianguang $ &"));

Ff display: C-sharp code
NAME: Zhang Jianguang {NAME} amp
IE display: C-sharp code
Name: Zhang Jianguang $ &
After testing, even if replace in ff does not use regular expressions, "$ &" also indicates backward matching !!!
Speechless!

Search for information, test, and summarize as follows:

I,

Character

Description

$

$

$ &

SpecifyStringObj.

$'

SpecifyStringObj.

$'

SpecifyStringObj.

Test code:

Copy codeThe Code is as follows:
Document. write ("NAME: {NAME }__". replace (/\ {NAME}/g, "$ &"));
Document. write ("NAME: {NAME }__". replace (/\ {NAME}/g, "Zhang Jianguang $ "));
Document. write ("NAME: {NAME }__". replace (/\ {NAME}/g, "$ '"));
Document. write ("NAME: {NAME }__". replace (/\ {NAME}/g, "$ '"));

Ie, ff, and chrome have the same results:
Copy codeThe Code is as follows:
NAME: Zhang Jianguang {NAME} amp _ NAME: Zhang Jianguang $ __name: Zhang Jianguang NAME :__ NAME: Zhang Jianguang ____

Ii. replace in ff and chrome does not use regular expressions, but also takes effect for the above special characters
In ie, replace does not use regular expressions and does not work for the preceding special characters. Test code:
Copy codeThe Code is as follows:
Document. write ("NAME: {NAME }__". replace ("{NAME}", "Zhang Jianguang $ &"));
Document. write ("NAME: {NAME }__". replace ("{NAME}", "Zhang Jianguang $ &"));
Document. write ("NAME: {NAME }__". replace ("{NAME}", "Zhang Jianguang $ "));
Document. write ("NAME: {NAME }__". replace ("{NAME}", "$ '"));
Document. write ("NAME: {NAME }__". replace ("{NAME}", "$ '"));

Ff and chrome results:
Copy codeThe Code is as follows:
NAME: Zhang Jianguang {NAME} amp _ NAME: Zhang Jianguang $ __name: Zhang Jianguang NAME :__ NAME: Zhang Jianguang ____

Ie result:
Copy codeThe Code is as follows:
Name: Zhang Jianguang $ & __ name: Zhang Jianguang $ __name: Zhang Jianguang $ '_ name: Zhang Jianguang $ '__

3. As a result, is it necessary for us to handle the above situations when applying replace and other regular-related functions?
Test code:
Copy codeThe Code is as follows:
Var str = "NAME: {NAME }";
Var str2 = "$ <jesse $ <";
Document. write (str. replace (/\ {NAME}/g, str2 ));
Document. write ("<br/> ");
Document. write (str. replace (// {NAME}/g, str2.replace (// $/g, '$'); // note the four "$"

Page display:
Copy codeThe Code is as follows:
NAME: {NAME} lt; jesse {NAME} lt;
Name: $ <jesse $ <

You can also test it by yourself:
Copy codeThe Code is as follows:
Var str = "NAME: {NAME }";
Var str2 = "$ <jesse $ <";
Document. write (str. replace (// {NAME}/g, str2.replace (// $/g, '$ ')));

The best practice is:
Copy codeThe Code is as follows:
Function tplReplace (str, json ){
Return str. replace (/{(\ w +)}/gi, function (a, B ){
Return B in json? Json [B]:;
}
}

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.