2

i want to create JavaScript regexp that get all matches

  {{match1}}{{match2}} notmatch {{match3}}

result will be

['{{match1}}','{{match2}}','{{match3}}']

what i tried so far

 var text='{{match1}}{{match2}} notmatch {{match3}}';
 const regex = /(?<=\{{)(.*?)(?=\}})/gm;
 var match = text.match(regex);

this is working fine with chrome but in Firefox, IE and edge i am getting this error

SCRIPT5018: Unexpected quantifier

any solutions.

Opt Prutal
  • 384
  • 4
  • 14

1 Answers1

1

You can try a more simplified regex like the following:
(Not sure about IE, but it should work for Chrome and Firefox.)

var text='{{match1}}{{match2}} notmatch {{match3}}';
const regex = /{{([^}]+)}}/gm;
var match = text.match(regex);
console.log(match);

Without the braces: (borrowed from here), (Barmar's suggestion).

var text='{{match1}}{{match2}} notmatch {{match3}}';
var regex = /{{([^}]+)}}/gm;
var matches;
while (matches = regex.exec(text)){
    console.log(matches[1]);
}
abmblob
  • 361
  • 2
  • 14
  • 1
    He doesn't want to include the braces in the match result. That's why he's using lookarounds. – Barmar Mar 10 '19 at 06:46
  • 2
    i thought i cant get it with the braces but now its done, i will update the question so others might find it useful thanks ABm Ruman for the answer. and Barmar thanks for spreading frustration – Opt Prutal Mar 10 '19 at 07:21
  • ooh, I thought you need to get rid of the braces. Guess I'll have to revert my changes too :) – abmblob Mar 10 '19 at 07:25