javascript - 从间隔的单词中删除空格?

标签 javascript node.js regex pcre

假设以下字符串:

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/

相关文章:

javascript - 如何将行传递给内部多个请求

c# - 使用正则表达式提取重复模式

javascript - 查找大于或等于 2014 的正则表达式

javascript - 如何将参数传递给 JavaScript 中 compose 函数内的每个函数

javascript - 添加一些缓动 jQuery

javascript - 使用本地存储连接器时 StrongLoop 中的 ng-lb 命令失败

node.js - Redis、Node.JS 和唯一 ID

node.js - Azure 移动服务表筛选器

regex - Bash 需要测试字母数字字符串

javascript - 任何人都可以解释为什么这个 jQuery.offset() 每次使用时都会发生变化吗?