如果数字之间有逗号,则用 javascript 替换逗号

Joe*_*erg 4 javascript regex replace

我试图替换每次出现的逗号“,”,但前提是它出现在两个数字/数字之间。

例如,“Text,10,10 text,40 text,10,60”应返回为“Text,1010 text,40 text,1060”,其中我替换在10,1010,60之间找到的逗号,但保留逗号正文之后。

var text = "Text, 10,10 text, 40 text, 10,60";
var nocomma = text.replace(/,/g, '');
console.log(nocomma);
Run Code Online (Sandbox Code Playgroud)

Cod*_*iac 6

您可以使用捕获组并替换

var text = "Text, 10,10 text, 40 text, 10,60";
var nocomma = text.replace(/(\d),(\d)/g, '$1$2');

console.log(nocomma);
Run Code Online (Sandbox Code Playgroud)

如果您使用的现代浏览器支持lookbehind,您也可以使用它

str.replace(/(?<=\d),(?=\d)/g,'')
Run Code Online (Sandbox Code Playgroud)