0

I need to split a string by whitespace, but not when the whitespace is found inside "()" or "[]". I found a several similar questions from here, but I couldn't find out the ideal solution.

The string I want to parse can look like this (square brackets can also be replaced with regular brackets):

(1) "Some text [more text]"

(2) "Some text[more text]"

I want them to be split up like this:

(1) ["Some", "text", "[more text]"]

(2) ["Some", "text[more text]"]

Javascript - divide by spaces unless within brackets - This question was quite similar and the answer works really well in the first (1) situation. But in the second (2) situation, it doesn't work so well. After splitting, it looks like this:

["Some", "text[more", "text]"]

Is there a simple way to achieve what I want?

Community
  • 1
  • 1

3 Answers3

1

These seem to work:

1: \[[^\]]+\]|\S+\[[^\]]+\]|\S+
2: \[[^\]]+\]|(\S(\[[^\]]+\])?)+
3: (\S+)?\[[^\]]+\]|\S+

According to Regex Hero the first one is by far superior, and Miguel's answer slightly faster still.

mkjeldsen
  • 2,053
  • 3
  • 23
  • 28
  • Thank you for your answer, but according to Chrome's console, this RegExp results in: ["Some", "text[more", "text]"] - which is the same result I achieved using the method found from the answer I linked. ["Some", "text[more text]"] would be the correct behavior. – Anna Jokela May 17 '12 at 10:15
  • Yes, that's actually worse than what you have. I'll give it another look unless somebody beats me to it. – mkjeldsen May 17 '12 at 10:21
  • There. Maybe there's a more efficient pattern. – mkjeldsen May 17 '12 at 10:28
0

Try (/(\w+ )|(\w+)?((\[|\().*(\]|\))$)/g)

var a = "Some text [more text]";
var b = "Some text[more text]";

The results are:

a.match(/(\w+ )|(\w+)?((\[|\().*(\]|\))$)/g)

["Some ", "text ", "[more text]"]

b.match(/(\w+ )|(\w+)?((\[|\().*(\]|\))$)/g)

["Some ", "text[more text]"]

Miguel Rodrigues
  • 917
  • 6
  • 19
0

Use this regex:

/(\w+)?(\([^\)]*?\)|\[[^\]]*?\])(\w+)?|\w+/g

Samples:

console.log( 'Some(1) text [more text]'.match(regex) );
//Output-> ["Some(1)", "text", "[more text]"]

console.log( '(2) Some text[more text]'.match(regex) );
//Output-> ["(2)", "Some", "text[more text]"]

console.log( '(3 45)Some text[more text]'.match(regex) );
//Output-> ["(3 45)Some", "text[more text]"]
Engineer
  • 47,849
  • 12
  • 88
  • 91