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

How to perform string replacement in js

coldplay.xixi
coldplay.xixiOriginal
2020-09-11 13:24:1134881browse

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("-","/")

The results are as follows:

 "yyyy/MM-dd-hh-mm-ss"

2,

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

The results are as follows:

"yyyy/MM/dd/hh/mm/ss"

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!

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