1

I have a string such as this one:

a=func(1, 2, 2), b='hey', c=foobar('text'), d=1

which I'd like to parse into its key-value components, so that I can obtain a list of

[['a', 'func(1, 2, 2)', ['b', '\'hey\''], ['c', 'foobar(\'text\')'], ['d', '1']]

My approach was to use this regex: (\w*) *= *([^=]*), (?=\w* *=) with a positive lookahead, but that's ignoring the last key-value pair (d=1). Any idea how I can make the positive lookahead optional?

orange
  • 7,755
  • 14
  • 75
  • 139

1 Answers1

2

Try using the regex pattern (\w+)=(.*?)(?:,\s*(?=\w+=)|$), and then capture all matches:

var input = "a=func(1, 2, 2), b='hey', c=foobar('text'), d=1";
var regex = /(\w+)=(.*?)(?:,\s*(?=\w+=)|$)/g;
var match = regex.exec(input);
var result = [];
while (match != null) {
    result.push(new Array(match[1], match[2]));
    match = regex.exec(input);
}
console.log(result);

Here is what the pattern does:

(\w+)               match AND capture a key
=                   match an =
(.*?)               then match AND capture anything, until we see
(?:,\s*(?=\w+=)|$)  a comma, followed by optional space, and the next key
                    OR the end of the input string

Then, we build your expected 2D array using the first capture group as the key, and the second capture group as the value.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks. What is `?:` doing? – orange May 20 '19 at 13:34
  • @orange Please read [What does ?: do in regex](https://stackoverflow.com/questions/3705842/what-does-do-in-regex). `?:` turns off the capture group for that term, because we aren't interested in capturing it (we only care about the LHS and RHS of the variable assignments). – Tim Biegeleisen May 20 '19 at 13:35
  • Wouldn't that mean we don't really need the `?=`? – orange May 21 '19 at 02:23
  • `(?=\w+=)` is a positive lookahead. `?=` does _not_ mean a question mark followed by `=`. Read about lookarounds in regex. – Tim Biegeleisen May 21 '19 at 02:50
  • If you define `(?:,\s*(?=\w+=)|$)` as a non-matching group, isn't the positive lookahead redundant (a non-matching group is kind of the same, that is _optional_)? (Btw, the positive lookahead was part of my initial suggestion...) – orange May 21 '19 at 11:30
  • No. The group is part of the pattern regardless of whether `?:` appears or not. – Tim Biegeleisen May 21 '19 at 12:10