How to replace strings in js

The method of string replacement in js: 1. Use regular expression, the syntax is [str.replace(“string to be replaced”, “new string”)]; 2. Use conventional replacement method, the syntax is [ str.replace(/string to be replaced/g, “new string”)].

Related graphic recommendation: js tutorial

The method of string replacement in js:

Two methods: regular & regular

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 result is as follows:

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

2,

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

The result is as follows:

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

In summary:

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

The post How to do string replacement in js first appeared on Lenix Blog .

This article is reprinted from https://blog.p2hp.com/archives/9083
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment