Home > Article > Web Front-end > How to perform string replacement in js
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")].
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 allRelated 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!