Test nickname: jesse $ <
View code: Javascript codeCopy 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 codeCopy codeThe Code is as follows: document. write ("NAME: {NAME}". replace (// \ {NAME}/g, "Zhang Jianguang $ &"));
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 codeCopy 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, "Zhang Jianguang $ &"));
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]:;
}
}