2

Assuming I've been given the following to match against:

insert 'hello world' into {This is a test}

I want to match on whitespaces and push every match to my string array, because I need to know the index of the text in the string.

Here comes the tricky part; whitespaces inside single quotes (') and inside curly brackets ({}) must be excluded

My desired result would be:

  1. insert
  2. 'hello world'
  3. into
  4. {this is a test}

So far I've been able to exclude the whitespaces inside single quotes, however I can't figure out how to combine it with the curly brackets.

My regex as of now:

\s(?=(?:[^']|'[^']')$)

Niekert
  • 889
  • 8
  • 18

2 Answers2

2

Quite tricky this one. I've thought about matching instead of splitting this time:

'[^']*'|\{[^\}]*\}|\S+

Let's explain it a bit:

'[^']*'     # match a quoted string
|           # or
\{[^\}]*\}  # match zero or more characters between curly brackets
|           # or
\S+         # match a non-white space character one or more times

Online demo

HamZa
  • 13,530
  • 11
  • 51
  • 70
1

Niekert, resurrecting this question because it had a simple solution that wasn't mentioned. This situation sounds very similar to Match (or replace) a pattern except in situations s1, s2, s3 etc.

Here's our simple regex:

{[^}]+}|( )

The left side of the alternation matches complete { ... } braces. We will ignore these matches. The right side matches and captures spaces to Group 1, and we know they are the right spaces because they were not matched by the expression on the left.

This program shows how to use the regex (see the results in the pane of the online demo):

<script>
var subject = "insert 'hello world' into {This is a test}";
var regex = /{[^}]+}|( )/g;
var match = regex.exec(subject);
replaced = subject.replace(regex, function(m, group1) {
    if (group1 == "" ) return m;
    else return "SplitHere";
});
splits = replaced.split("SplitHere");
document.write("*** Splits ***<br>");
for (key in splits) document.write(splits[key],"<br>");
</script>

Reference

How to match (or replace) a pattern except in situations s1, s2, s3...

Community
  • 1
  • 1
zx81
  • 38,175
  • 8
  • 76
  • 97