假设以下字符串:
hello world, h e l l o! hi. I am happy to see you!
有没有办法像这样删除间隔单词中的空格?:
hello world, hello! hi. I am happy to see you!
我尝试了 [^ ]+(\s)
但捕获组匹配所有空格。谢谢。
最佳答案
一种正则表达式方法可能是:
var input = "hello world, h e l l o! hi. I am happy to see you!";
var output = input.replace(/(?<=\b\w)\s+(?=\w\b)/g, "");
console.log(output);
这里是对正则表达式模式的解释,它针对位于两侧两个独立的单个单词字符之间的空白:
(?<=\b\w) assert that what precedes is a single character
\s+ match one or more whitespace characters
(?=\w\b) assert that what follows is a single character
关于javascript - 从间隔的单词中删除空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68767077/