關於JS替換字串中字元

2021-06-22 08:11:27 字數 540 閱讀 1564

有時候用js替換字串時,replytext= replytext.replace("aa","");

這樣只能替換掉字串中第乙個aa,有時候需要替換全部的aa,這種方法就不能使用了。。

可以用如下方法做參考:

function del_html_tags(str,reallydo,replacewith)

str是目標字串

reallydo是替換誰

replacewith是替換成什麼。

var replytext="恭恭敬敬恭恭敬敬恭恭敬敬

恭恭敬敬恭恭敬敬恭恭敬敬恭恭敬敬

";eg:

replytext= del_html_tags(replytext,"

"," ");      

replytext= del_html_tags(replytext,"

replytext= del_html_tags(replytext,"

","");

就可以得到

"恭恭敬敬恭恭敬敬恭恭敬敬  恭恭敬敬恭恭敬敬恭恭敬敬恭恭敬敬"

js中替換字串

摘自 function formatstr str 要注意兩點 要使用正規表示式,不能使用 str.replace r n newstring 這會導致只替換第乙個匹配的子字串。母字串中不一定 r n 會同時存在,也許只有 n,沒有 r 也是可能的。js 比我聰明的你,看完上面的例子之後,會發現第二...

js中替換字串

js中替換字串 數字人 發表於 2012 3 20 20 23 53 function formatstr str 要注意兩點 要使用正規表示式,不能使用 str.replace r n newstring 這會導致只替換第乙個匹配的子字串。母字串中不一定 r n 會同時存在,也許只有 n,沒有 r...

js中替換字串

function formatstr str 要注意兩點 要使用正規表示式,不能使用 str.replace r n newstring 這會導致只替換第乙個匹配的子字串。母字串中不一定 r n 會同時存在,也許只有 n,沒有 r 也是可能的。替換所有空格 function replacespace...