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

How to replace all strings with jQuery

高洛峰
Release: 2016-12-28 09:59:40
Original
1430 people have browsed it

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/>
Copy after login

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
Copy after login

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!

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