有时候用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,"
","");
就可以得到
"恭恭敬敬恭恭敬敬恭恭敬敬 恭恭敬敬恭恭敬敬恭恭敬敬恭恭敬敬"