小编Kln*_*h13的帖子

正则表达式选择引号中的所有空格?

例如:

string text = 'some text "and some more" and some other "this is a second group" okay the end';.

我想捕获引号之间的所有空格。最终目标是用逗号替换这些空格。

最终目标,例如:

'some text "and,some,more" and some other "this,is,a,second,group" okay the end'
Run Code Online (Sandbox Code Playgroud)

例如,这将在 javascript 中执行我想要的操作:

text.replace(/(["]).*?\1/gm, function ($0) {
    return $0.replace(/\s/g, ',');
});
Run Code Online (Sandbox Code Playgroud)

不幸的是,我唯一可用的工具是 textmate 的查找/替换功能。

我发现另一个与我需要的相反,但使用了我需要的一行:

text.replace(/\s+(?=([^"]*"[^"]*")*[^"]*$)/gm, ',');
Run Code Online (Sandbox Code Playgroud)

谢谢!

javascript regex textmate

6
推荐指数
1
解决办法
3438
查看次数

标签 统计

javascript ×1

regex ×1

textmate ×1