0

I have a regular expression to pick the number. For ?page=2& this will return 2. My Expression is working fine for Chrome and Mozilla but not working on Safari.

Expression is: /(?<=page=)(.*?)(?=&)/

Please let me know what am I doing wrong here.

  • You've a lookbehind in your expression, which is something that isn't supported in Safari yet. – Kevin Jul 14 '22 at 09:32
  • Hi, I have an idea about this. But I am not sure how to write it without the lookbehind. If possible can you suggest the regex? – Parag Singh Jul 14 '22 at 09:57
  • As you are already using a capture group, you can also just match it `page=(.*?)&` or perhaps even better `page=([^&\n]*)&` – The fourth bird Jul 14 '22 at 11:08
  • See https://stackoverflow.com/questions/58460501/js-regex-lookbehind-not-working-in-firefox-and-safari or https://stackoverflow.com/questions/641407/negative-lookbehind-equivalent-in-javascript – The fourth bird Jul 14 '22 at 11:12

1 Answers1

0

As look-arounds are not yet supported on Safari, capture the page= part, but then use a capture group to extract the part of interest:

const url = "example.com?page=2&chapter=9";
const page = /\bpage=([^&]*)/.exec(url)[1];

console.log(page);
trincot
  • 317,000
  • 35
  • 244
  • 286