JavaScript的replace (方法直接为str.replace ) '-','!' 使用时,只会替换第一个匹配的字符。
以及str.replace(/(-/g,'!' )可以替换所有匹配的字符。 g是全球标志。
替换() )
thereplace (methodreturnsthestringthatresultswhenyoureplacetextmatchingitsfirstargument
(a规则表达式) withthetextofthesecondargument (a字符串)。
iftheg (全局) flagisnotsetintheregularexpressiondeclaration,this method replaces only the first
occurrenceofthepattern.for example,
var s='Hello. Regexps are fun.'; s=s.replace(/)./,'!' ); //replacefirstperiodwithanexclamationpointalert (s;
produces the string “Hello! regexpsarefun." includingthegflagwillcausetheinterpreterto "
perform a全局替换,findingandreplacingeverymatchingsubstring.for example,
var s='Hello. Regexps are fun.'; s=s.replace(/)./g,'!' ); //replaceallperiodswithexclamationpointsalert (s;
YeldsthisResult3360「Hello! Regexps are fun! " "
所以,有以下方法。
string.replace(/reallydo/g,replaceWith );
string.replace(newregexp ) reallydo,' g ',replaceWith );
string :字符串表达式包含要替换的子字符串。
reallyDo :搜索到的子字符串。
替换宽度:要用于替换的子字符串。
日本铁路代码
以上js置换字符串中指定的所有字符(实现代码)是编辑者共享给大家的所有内容。 希望能作为参考。 另外,我想支持编程之家。
相关报道
总结
如果你觉得编程家庭网站的内容很好,欢迎程序员朋友推荐程序员网站。
该文字内容由网民网络收集整理提供,作为学习参考,版权归原作者所有。
如果你喜欢交流学习经验,点击链接加入交流1群: 1065694478 (满)交流2群: 163560250