我想将 html 字符串中的多个“”替换为仅一个空格。但是,它会将每个“ ”替换为空格,从而产生许多空格。例如,
htmlValue.replace(/ /gi, " ");//" " becomes " ". (2spaces) but I want only 1 space
最佳答案
你可以试试这个:
/( )+/gm
并替换为单个“”
示例代码:
const regex = /( )+/gm;
const str = ` sfdasdf `;
const subst = ` `;
const result = str.replace(regex, subst);
console.log(result);
关于jquery - 如何替换字符串 jquery 中的任意数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41095412/