如何逐个字符串替换(不是每个数字出现)?

时间:2017-03-23 05:56:49

标签: javascript regex string

所以这就是我想要做的事情:

 var string = "/something/12/anything/234/xyz";
 //i want to get this to result out of this ==> "/something/secret/anything/secret/xyz

基本上用一些字符串替换所有数字" secret"。

1 个答案:

答案 0 :(得分:1)

使用正则表达式/\d+/g替换

var str = "/something/12/anything/234/xyz";

console.log(str.replace(/\d+/g, "secret"));