Home  >  Article  >  Web Front-end  >  How to replace all strings with jQuery

How to replace all strings with jQuery

高洛峰
高洛峰Original
2016-12-28 09:59:401409browse

The example in this article describes the jQuery method of replacing all strings. Share it with everyone for your reference, the details are as follows:

Unlike the Replace method of the C# String type, jQuery's Replace can only replace the first matching content.

For example:

var str = "a<br/>b<br/>c<br/>";
var Newstr = str.Replace("<br/>", "");
alert(Newstr); //内容为:ab<br/>c<br/>

To replace all matching items, you can use regular expressions:

var str = "a<br/>b<br/>c<br/>";
re = new RegExp("<br/>","g"); //定义正则表达式
//第一个参数是要替换掉的内容,第二个参数"g"表示替换全部(global)。
var Newstr = str.Replace(re, ""); //第一个参数是正则表达式。
//本例会将全部匹配项替换为第二个参数。
alert(Newstr); //内容为:abc

I hope this article will be helpful to everyone in jQuery programming.

For more jQuery methods to replace all strings, please pay attention to the PHP Chinese website!

Statement:
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