1

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?

Mechanic
  • 5,015
  • 4
  • 15
  • 38
Raj
  • 368
  • 1
  • 5
  • 17
  • 1
    Are you only interested to use `.test` on it? Then just remove the `?<=` part, so it starts with `/(api-)`. There is no need to use look behind; just capture it. BTW: all the code you have around it seems unrelated. Why is it in the question? – trincot Mar 13 '20 at 20:08
  • I have webapp doing this , to make it easy shorter and executable I made this quickly – Raj Mar 13 '20 at 21:06

1 Answers1

2

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...

Mechanic
  • 5,015
  • 4
  • 15
  • 38