Home > Web Front-end > JS Tutorial > body text

How to replace all specified strings in js

藏色散人
Release: 2021-02-02 14:24:20
Original
22296 people have browsed it

JS method to replace all specified strings: first open the corresponding js file; then perform global replacement through the "str.replace(/\
/g, '!')" method. \>

How to replace all specified strings in js

The operating environment of this tutorial: Windows 7 system, Dell G3 computer.

js Replace all specified strings

The replace method of js only replaces the first matching character

If you want to replace globally, Use the following method (g is the global flag)

str.replace(/需要替换的字符串/g,"新字符串")
//如果有特殊符号,使用‘\’转义就行了,比如把换行符‘<br/>’替换成“!”
str.replace(/\<br\/\>/g, &#39;!&#39;)
Copy after login

Recommended: "js Basic Tutorial"

The above is the detailed content of How to replace all specified strings in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
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