3

I am looking to split a string by several separators, but I would like to include the separators in the returned array. For example I want to split this string "hello i=5 (goodbye)" by spaces, assignment operators, and parenthesis. The only way I know how to split that string is by doing something like this: "hello i=5 (goodbye)".split(/[\s=)(]/); but the returned array is ["hello", "i", "5", "", "goodbye", ""]. Is there a way to include the separators in the result? I would like it to return ["hello", "i", "=", "5", "(", "goodbye", ")"]

Thanks for the help!

Joey
  • 1,144
  • 3
  • 15
  • 29
  • Interesting fact from the MDN documentation: *"If separator is a regular expression that contains capturing parentheses, then each time separator is matched the results (including any undefined results) of the capturing parentheses are spliced into the output array. However, not all browsers support this capability."* – Felix Kling Feb 09 '13 at 00:40

3 Answers3

3

You can use match and match for the symbols you want and groups that don't match the symbols you want in a single regexp:

"hello i=5 (goodbye)".match(/[\s=)(]|[^\s=)(]+/g)
// result: ["hello", " ", "i", "=", "5", " ", "(", "goodbye", ")"]

If you want to get rid of the spaces you can chain .filter(function(el){ return el.trim(); }) to the above.

mVChr
  • 49,587
  • 11
  • 107
  • 104
  • I ended up modifying that a little bit to get what I want: `"hello i=5 (goodbye)".match(/[^\s=\(\)]+|[=\(\)]/g)` with a result: `["hello", "i", "=", "5", "(", "goodbye", ")"]` – Joey Feb 09 '13 at 00:48
0

You could use lookahead, which selects an empty string right before the matching character.

/(?=[\s=)(])/

For lookbehind, to select the empty string following a metch, use one of these methods.

Hope this helps!!

ameed
  • 1,132
  • 6
  • 25
0
"hello i=5 (goodbye)".replace(/[\s=)(]/g, function(a){return ","+a+","}).split(/,/);

Output:

[hello,  , i, =, 5,  , , (, goodbye, ), ]

The trick is to introduce another separator such as a comma. Note that the new separator must not be present in the original string.

Terry Li
  • 16,870
  • 30
  • 89
  • 134