JS replaces all characters in a string instead of the first

  • 2020-03-30 03:11:23
  • OfStack

Sometimes when replacing a string with JS, replyText= replytext.replace ("aa","");

This can only replace the first aa in the string, sometimes need to replace the whole aa, this method is not used.

The following methods can be used for reference:
 
function del_html_tags(str,reallyDo,replaceWith) { 
var e=new RegExp(reallyDo,"g"); 
words = str.replace(e, replaceWith); 
return words; 
} 

STR is the target string

ReallyDo is the replacement

ReplaceWith is what you replaceWith.

Var replyText = "< P> To be respectfully respectfully < / p> < P> < Br / > < / p> < P> To be respectfully respectfully to be respectfully respectfully < / p> < P> < Br / > < / p>" ;

Eg:
 
replyText= del_html_tags(replyText,"<br />"," "); 

replyText= del_html_tags(replyText,"<p>",""); 

replyText= del_html_tags(replyText,"</p>",""); 

You can get

"With all due respect. With all due respect. With all due respect."

Related articles: