您好,欢迎访问一九零五行业门户网

JS替换字符串中字符即替换全部而不是第一个_javascript技巧

有时候用js替换字符串时,replytext= replytext.replace(aa,);
这样只能替换掉字符串中第一个aa,有时候需要替换全部的aa,这种方法就不能使用了。。
可以用如下方法做参考:
复制代码 代码如下:
function del_html_tags(str,reallydo,replacewith) {
var e=new regexp(reallydo,g);
words = str.replace(e, replacewith);
return words;
}
str是目标字符串
reallydo是替换谁
replacewith是替换成什么。
var replytext=恭恭敬敬恭恭敬敬恭恭敬敬
恭恭敬敬恭恭敬敬恭恭敬敬恭恭敬敬
;
eg:
复制代码 代码如下:
replytext= del_html_tags(replytext,
, );
replytext= del_html_tags(replytext,,);
replytext= del_html_tags(replytext,
,);
就可以得到
恭恭敬敬恭恭敬敬恭恭敬敬 恭恭敬敬恭恭敬敬恭恭敬敬恭恭敬敬
其它类似信息

推荐信息