Home > Web Front-end > JS Tutorial > JS replaces characters in a string by replacing all characters instead of the first one_javascript tips

JS replaces characters in a string by replacing all characters instead of the first one_javascript tips

WBOY
Release: 2016-05-16 16:46:23
Original
1573 people have browsed it

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

This can only replace the first aa in the string. Sometimes it is necessary to replace all aa. This method cannot be used. .

You can use the following method for reference:

Copy the code The code is as follows:

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 who to replace

replaceWith is what to replace with.

var replyText="

Respectfully, respectfully, respectfully, respectfully, respectfully


Respectfully, respectfully Respectfully, respectfully, respectfully, respectfully


";

eg:
Copy code The code is as follows:

replyText= del_html_tags(replyText,"
"," ");

replyText= del_html_tags(replyText,"

","");

replyText= del_html_tags(replyText,"

","");

You can get

"Respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, respectfully, and respectfully"
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template