简体   繁体   English

JavaScript正则表达式仅在特定上下文中按空格分割

[英]JavaScript regular expression split by space only in certain context

Given the string: word word{test test} 给定字符串: word word{test test}

How can I split this into an array like: 我如何将其拆分为一个数组,如:

["word", "word{test test}"]

I want to split by space but ignore the space inside the curly braces 我想按空格分开,但忽略花括号内的空格

Instead of thinking about this as a split, I think it's easier to think about it as a match. 与其将其视为分裂,不如将其视为匹配更为容易。 The following is the RegEx I've concocted. 以下是我炮制的RegEx。 The line of code you can use is: 您可以使用的代码行是:

str.match(/[^\s]*{[^}]*}[^\s]*|[^\s{}]+/g)

To break it down more readable there are two parts. 要使其更易读,可以分为两个部分。 The first: 首先:

[^\s]* { [^}]* } [^\s]*

it says any number of non-whitespace followed by a { followed by anything that isn't a } then a } then any number of non-whitespace. 它表示任意数量的非空白,后跟{,然后是不是}的任何东西,然后是},然后是任意数量的非空白。 So I think you'll have to assume that every start brace is capped. 因此,我认为您必须假设每个起始括号都被限制了。 Not sure if you can assume that or not. 不知道是否可以假设。 If you need to match nested braces then you need to use something more powerful than Regex/FA because it does not have state. 如果需要匹配嵌套的花括号,则需要使用比Regex / FA更强大的功能,因为它没有状态。

The second part is 第二部分是

[^\s { }]+

saying match one or more of any non whitespace/non curly brace item. 说匹配任何非空格/非大括号项目中的一项或多项。

Here is a jsfiddle showing the code. 这是显示代码的jsfiddle。 JSFiddle 的jsfiddle

You can't do this with a pure JavaScript regex. 您不能使用纯JavaScript正则表达式来执行此操作。 I am going to eat my words now however, as you can use the following solution using callback parameters: 但是,我现在要吃我的话,因为您可以使用带有回调参数的以下解决方案:

var regex = /{[^}]+}|( )/g
replaced = subject.replace(regex, function($0, $1) {
    if ($1 == " ") return "\0";
    else return $0;
});
splits = replaced.split("\0");
 >>> subject = "word word{test test}" ... "word word{test test}" >>> var regex = /{[^}]+}|( )/g replaced = subject.replace(regex, function($0, $1) { if ($1 == " ") return "\\0"; else return $0; }); splits = replaced.split("\\0"); ... ["word", "word{test test}"] 

Read more: 阅读更多:

It's not foolproof, but you can split on one or more space characters only if there is no } before a { ahead in the string. 这不是万无一失的方法,但是只有在字符串的{之前没有}才可以分割一个或多个空格字符。

var str = 'word word{test test}';

str.split( /\s+(?![^{]*})/ );   
// ["word", "word{test test}"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM