Works in Chrome, but breaks in Safari: Invalid regular expression: invalid group specifier name /(?<=\/)([^#]+)(?=#*)/

JavascriptRegexSafari

Javascript Problem Overview


In my Javascript code, this regex /(?<=\/)([^#]+)(?=#*)/ works fine in Chrome, but in safari, I get:

> Invalid regular expression: invalid group specifier name

Any ideas?

Javascript Solutions


Solution 1 - Javascript

Looks like Safari doesn't support lookbehind yet (that is, your (?<=\/)). One alternative would be to put the / that comes before in a non-captured group, and then extract only the first group (the content after the / and before the #).

/(?:\/)([^#]+)(?=#*)/

Also, (?=#*) is odd - you probably want to lookahead for something (such as # or the end of the string), rather than a * quantifier (zero or more occurrences of #). It might be better to use something like

/(?:\/)([^#]+)(?=#|$)/

or just omit the lookahead entirely (because the ([^#]+) is greedy), depending on your circumstances.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontechguy2000View Question on Stackoverflow
Solution 1 - JavascriptCertainPerformanceView Answer on Stackoverflow