Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何用字符串替换数字(不是每次出现数字)?_Javascript_Regex_String - Fatal编程技术网

Javascript 如何用字符串替换数字(不是每次出现数字)?

Javascript 如何用字符串替换数字(不是每次出现数字)?,javascript,regex,string,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”替换所有数字。使用regex/\d+/g替换 var str=“/something/12/anything/234/xyz”; console.log(str.re

所以我想做一件事:

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

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

使用regex
/\d+/g
替换

var str=“/something/12/anything/234/xyz”;

console.log(str.replace(/\d+/g,“secret”)@KobyDouek no它将生成类似“/something/xx/anyhing/xxx/xyz”的刺,这取决于数字,如果有23,它将是xx,如果有123,它将是xxx。我想要的是任何数字123或23,它应该被替换为秘密。