Such as
| The code is as follows |
Copy Code |
var str = "Everybody is a man, isn't he?" str = str.replace (' Yes ', ' well '); alert (str); |
Only the first "?" is replaced in the above code, and the output is "Everyone is a man, isn't it?"
Replacing a regular expression is a good idea if you want to replace all eligible content in a string, and you can replace it with the following code
| The code is as follows |
Copy Code |
var str= ' Everyone is a man, isn't it? ' str = str.replace (//g, ' well '); alert (str); |
This will output "Everyone is a man, is not it?", all eligible content has been replaced, particularly to note that the regular expression//g Here's G, this g represents the global, does not give this configuration item is not all replaced.
Cases
| The code is as follows |
Copy Code |
<script type= "Text/javascript" > var str= "Www.111cn.netaa" document.write (Str.replace (/aa/, ""))//output result is www.111cn.net </script> |
Cases
| code is as follows |
copy code |
| //ensure word " JavaScript "is correctly case-sensitive Text.replace (/javascript/i, ' JavaScript '); ////Replace all double quotes with a pair of front and back single quotes Text.replace ([^] "/g," "'" "); //Converts a separate name from the format "Mack, Xu" to "Xu Mack" Name.replace (/(w+) s*,s* (w+)/, "$ $"); //capitalize the first letter of all words in a string Text.replace (/bw+b/g, function (word) { return word.substring (0, 1). toUpperCase () + Word.substring (1); }); |