Given these examples:
{{foo}}
{foo{bar}}
{bar}
baz
{quz
{foobar}}
the expected output will be:
foo
foo{bar}
bar
baz
{quz
foobar}
That is to say, search in arbitrary text witch may contains up to one match, and replace the balanced delimeter ({}
) if there are any by using regular expression with the string replace
method.
someStr.replace(/regexp goes here/, match => return match);
I know pattern for brackets pair ({}
) /\{(.*?)\}/
, ({{}}
) /\{\{(.*?)\}\}/
, but don't know how to combine the 0, 1 and 2 occurrences for the brackets into one regular expression.
Also, in order to only match the content without the delimeter, we should use the lookaround assertions https://stackoverflow.com/a/2973495/1553656