Home > Web Front-end > JS Tutorial > How to perform string replacement in js

How to perform string replacement in js

coldplay.xixi
Release: 2020-09-11 13:24:11
Original
35009 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:
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
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template