Raj
Raj

Reputation: 468

Regex works on chrome but returns invalid error on safari

text = document.getElementById("p01").innerHTML; 
document.getElementById("demo").innerHTML = /(?<=api-)(.*)-us(.*)/.test('api-stg-usw2b');
<h2>JavaScript Regular Expressions</h2>

<p>Search for an "e" in the next paragraph:</p>

<p id="p01">The best things in life are free!</p>

<p id="demo"></p>

This regex works on chrome but fails on safari… I tried to replace < with : but also it didn't work

This is the error I see: SyntaxError: Invalid regular expression: invalid group specifier name

Not sure how to fix it? What is the regex cheat sheet I have to use?

Upvotes: 1

Views: 5401

Answers (1)

Mechanic
Mechanic

Reputation: 5380

This first capturing group is invalid (also doesn't work on Firefox); positive look behind (?<=.. is not supported in the javscript flavor of regex; and its browser dependent implementation; check out this SO answer for further reading; so try to use a more widely supported "pattern" instead of /(?<=api-)(.*)-us(.*)/; it definitely can be written simpler without positive look behind; I also suggest try your pattern in a online regex playground like regexr or regex101, etc...

Upvotes: 2

Related Questions