js代码删除字符串中数字之间的空格

Chr*_*ine 3 javascript

有什么方法可以删除字符串中的空格吗?

var str = "商店是 5 6 7 8"

处理后输出应该是这样的:“商店是5678”

这该怎么做?

Gab*_*leu 5

这是非常接近的桑托斯·辛格回答,但不会取代之间的空间is5

const regex = /(\d)\s+(?=\d)/g;
const str = `The store is 5 6 7 8`;
const subst = `$1`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);
Run Code Online (Sandbox Code Playgroud)

https://regex101.com/r/nSZfQR/3/


Sul*_*han 0

你好你可以试试这个。

var str = "The store is 5 6 7 8"
var regex = /(\d+)/g;
str.split(/[0-9]+/).join("") + str.match(regex).join('')
Run Code Online (Sandbox Code Playgroud)

希望这会起作用。