]
The following is replaced by array regular expression Implemented code
If you need to introduce external Js, you need to refresh to execute
]
"hand hand hand" think Transform to "hand.gif hand.gif hand.gif"
Start with
str=str.replace("hand","hand.gif");
Output: hand.gif hand hand
replaced only once. . . :(
I thought of using regular expressions, because replace can be replaced with regular expressions.
Quote
Returns a copy of the string after text replacement according to the regular expression.
stringObj .replace(rgExp, replaceText)
So write
str = str.replace(/hand/,"hand.gif")
to replace all. Add g,
str = str.replace(/hand/g, "hand.gif")
Still not working: (
Refer to JavaScript's replace method and regular expression to explain this After reading this article, I finally realized that it turns out that the stuff in () needs to be enclosed in (). The correct way to write it is as follows:
str = "hand hand hand";
str=str.replace((). /(hand)/g,"hand.gif");
document.write(str);
Correct output: hand.gif hand.gif hand.gif.
JS regular expression. One way to write it is to use RegExp:
such as str=str.replace(/(hand)/g,"hand.gif");
is equivalent to:
reg = new RegExp("(hand)" ,"g");
str = str.replace(reg,'hand.gif');
This method is more suitable when reg needs to be generated dynamically.
Expand it:
str = "hand'( hand'( hand'(";
str=str.replace(/(hand'()/g,"hand.gif");
document.write(str);
str = 'hand'( hand'( hand'(';
str=str.replace(/(hand'()/g,"hand.gif"); document. write(str);