Alert ("abacacf". replace ('A', '9 '));
Alert ("abacacf". replace (/a/g, '9 '));
Result 9 of the first run: bacaf, which replaces the first
The second running result 9b9c9f can be used to replace all js functions.
In fact, the second point is to use a regular expression to replace g globally to represent gobal.
Appendix: Global replacement with special symbolsCopy codeThe Code is as follows: <! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<Meta name = "Generator" CONTENT = "EditPlus">
<Meta name = "Author" CONTENT = "">
<Meta name = "Keywords" CONTENT = "">
<Meta name = "Description" CONTENT = "">
<Script language = "javascript" type = "text/javascript">
String. prototype. replaceAll = stringReplaceAll;
Function stringReplaceAll (AFindText, ARepText ){
Var raRegExp = new RegExp (AFindText. replace (/([\ (\) \ [\] \ {\} \ ^ \ $ \ + \-\*\? \. \ "\ '\ | \/\])/G," \ $1 ")," ig ");
Return this. replace (raRegExp, ARepText );
}
Function myreplace (){
Var content = document. getElementById ("content"). value;
Var rel_con = content. replaceAll ("$ name", "wwww ");
Document. getElementById ("content2"). value = rel_con;
}
</Script>
</HEAD>
<BODY>
<Input type = "text" id = "content" name = "contxt" value = "$ name customer name"/> <input type = "button" value = "replace"
Onclick = "myreplace ()"/> <br/>
<Input type = "text" id = "content2" value = ""/>
</BODY>
</HTML>