Javascript Regex - 忽略 2 个字符之间的某些字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20114005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:45:41  来源:igfitidea点击:

Javascript Regex - ignoring certain characters between 2 chars

javascriptregex

提问by AlvinfromDiaspar

I have a need to split a string on space character (' ') but while excluding any spacesthat come within 2 specific characters (say single quotes).

我需要在空格字符 (' ') 上拆分字符串,但同时排除2 个特定字符内的任何空格(例如单引号)。

Here is an example string:

这是一个示例字符串:

This-is-first-token This-is-second-token 'This is third token'

这是第一个令牌 这是第二个令牌 '这是第三个令牌'

The output array should look like this:

输出数组应如下所示:

[0] = This-is-first-token
[1] = This-is-second-token
[2] = 'This is third token'

Question: Can this be done elegantly with regular expression?

问题:这可以用正则表达式优雅地完成吗?

回答by elixenide

Short Answer:

简答:

A simple regex for this purpose would be:

为此目的的简单正则表达式是:

/'[^']+'|[^\s]+/g

Sample code:

示例代码:

data = "This-is-first-token This-is-second-token 'This is third token'";
data.match(/'[^']+'|[^\s]+/g);

Result:

结果:

["This-is-first-token", "This-is-second-token", "'This is third token'"]

Explanation:

解释:

Regular expression visualization

正则表达式可视化

Debuggex Demo

调试器演示

I think this is as simple as you can make it in just a regex.

我认为这很简单,您只需使用正则表达式即可。

The gat the end makes it a global match, so you get all three matches. Without it, you get only the first string.

g让你获得所有三场比赛在最后成为全球性的比赛。没有它,你只能得到第一个字符串。

\smatches all whitespace (basically, and tabs, in this instance). So, it would work even if there was a tab between This-is-first-tokenand This-is-second-token.

\s匹配所有空格(在本例中基本上和制表符)。因此,即使This-is-first-token和之间有选项卡,它也能工作This-is-second-token

To match content in braces, use this:

要匹配大括号中的内容,请使用:

data.match(/\{[^\}]+\}|[^\s]+/g);

Regular expression visualization

正则表达式可视化

Debuggex Demo

调试器演示

Braces or single quotes:

大括号或单引号:

data.match(/\{[^\}]+\}|'[^']+'|[^\s]+/g);

Regular expression visualization

正则表达式可视化

Debuggex Demo

调试器演示

回答by anubhava

You can use this split:

您可以使用此拆分:

var string = "This-is-first-token This-is-second-token 'This is third token'";
var arr = string.split(/(?=(?:(?:[^']*'){2})*[^']*$)\s+/);
//=> ["This-is-first-token", "This-is-second-token", "'This is third token'"]

This assumes quotes are all balanced.

这假设报价都是平衡的。

回答by Rob M.

I came up with the following:

我想出了以下内容:

"This-is-first-token This-is-second-token 'This is third token'".match(/('[A-Za-z\s^-]+'|[A-Za-z\-]+)/g)
["This-is-first-token", "This-is-second-token", "'This is third token'"]