-2

I need to match a specific regex syntax and split them so that we can match them to an equivalent value from a dictionary.

Input:

{Expr "string"}
{Expr "string"}{Expr}

Current code:

value.match(/\{.*\}$/g)

Desired Output:

[{Expr "string"}]
[{Expr "string"},{Expr}]
nicho-reyes
  • 63
  • 1
  • 4

2 Answers2

0

One option, assuming your version of JavaScript support it, would be to split the input on the following regex pattern:

(?<=\})(?=\{)

This says to split at each }{ junction between two terms.

var input = "{Expr \"string\"}{Expr}";
var parts = input.split(/(?<=\})(?=\{)/);
console.log(parts);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Use a non-greedy quantifier .*?. And don't use $, because that forces it to match all the way to the end of the string.

value = '{Expr "string"}{Expr}'
console.log(value.match(/\{.*?\}/g));
Barmar
  • 741,623
  • 53
  • 500
  • 612