ChatGPT解决这个技术问题 Extra ChatGPT

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?

It seems that Safari is not yet compliant with the 2018 standard. There's a bug report here that is over 4 years old!
Year 2022. Still an issue with Safari

G
Gleb Kemarsky

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.


Thanks for this. I need to get the index 1 from the exec result rather than index 0. But it works.
Great! Works for me! To validate only numbers and one plus signal (+) (phone number with possible indicative) i have /(?<=\+.*)\+/g and changed to /(?:\+.*)\+/g. Now it works also in Safari!
can you please help me to make this compatible with safari. /\B(?
@AramBecker Yes, they're all built on Webkit. If Apple allowed other engines to run on their OS, some (or all) of those other engines would very likely support it by now.
Adding this to the long, long list of things to throw at people who claim that Safari is not the new Internet Explorer.
S
Soviut

Just wanted to put this out there for anyone who stumbles across this issue and can't find anything...

I had the same issue, and it turned out to be a RegEx expression in one of my dependencies, namely Discord.js .

Luckily I no longer needed that package but if you do, consider putting an issue out there or something (maybe you shouldn't even be running discord.js in your frontend react app).