Skip to content
Advertisement

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

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

Invalid regular expression: invalid group specifier name

Any ideas?

Answer

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.

Advertisement