Skip to content Skip to sidebar Skip to footer

JavaScript Regular Expression Split By Space Only In Certain Context

Given the string: 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

Solution 1:

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. 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.

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


Solution 2:

You can't do this with a pure JavaScript regex. 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:


Solution 3:

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}"]

Post a Comment for "JavaScript Regular Expression Split By Space Only In Certain Context"