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

How to perform string replacement in js

coldplay.xixi
Release: 2020-09-11 13:24:11
Original
34782 people have browsed it

Methods for string replacement in js: 1. Use regular expressions, the syntax is [str.replace("String to be replaced", "New String")]; 2. Use regular Replacement method, the syntax is [str.replace(/string to be replaced/g, "new string")].

How to perform string replacement in js

Recommended related pictures and texts: js tutorial

js Methods for string replacement in:

Two methods: Regular & Conventional

##str.replace("String to be replaced" , "new string")

##str.replace(/String to be replaced/g, "new string")

For example:

1,

"yyyy-MM-dd-hh-mm-ss".replace("-","/")
Copy after login

The results are as follows:

 "yyyy/MM-dd-hh-mm-ss"
Copy after login

2,

"yyyy-MM-dd-hh-mm-ss".replace(/-/g,"/")
Copy after login

The results are as follows:

"yyyy/MM/dd/hh/mm/ss"
Copy after login

In summary:

Regular replacement will only replace the first matching character, regular replacement can replace all

Related learning recommendations:
js video tutorial

The above is the detailed content of How to perform string replacement in js. For more information, please follow other related articles on 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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!