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

5

178

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

Invalid regular expression: invalid group specifier name

Any ideas?

Symbolize answered 28/7, 2018 at 6:46 Comment(14)
Related: #7376738Damascus
Related too: #4200657Damascus
It seems that Safari is not yet compliant with the 2018 standard. There's a bug report here that is over 4 years old!Boogie
Year 2022. Still an issue with SafariMichel
sometimes I wonder what goes on at appleIceboat
Hey, I'm from future, 2167 outside, still an issueGlennisglennon
Year 2023. Still an Issue with Safari, lol.Patrica
As of Jan 12, The latest Safari Technology Preview release 161 (bugs.webkit.org/show_bug.cgi?id=174931#c56) supports lookbehind.Demolish
Year 2024. Still an Issue with Safari (mass release).Recor
@SanjayVerma go back to the future!Pyrostat
Safari, the badarse.Sfax
year 2023-May, still an issue with safariSilvana
Safari is the new IEAwad
Year 2023-Aug. Still an Issue with Safari.Effuse
A
234

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.

Alti answered 28/7, 2018 at 6:50 Comment(11)
Thanks for this. I need to get the index 1 from the exec result rather than index 0. But it works.Symbolize
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!Oliva
can you please help me to make this compatible with safari. /\B(?<!\.\d*)(?=(\d{3})+(?!\d))/gOverall
I think this is a similar issue, can you please help? #68274461Mahlstick
Actually, this doesn't seem to be a Safari problem, but some underlying issue of the iOs operating system. This consistently breaks my pages across Safari, Chrome, Firefox and Opera (granted 3 of these use the same engine).Golden
@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.Alti
Adding this to the long, long list of things to throw at people who claim that Safari is not the new Internet Explorer.Prase
@Symbolize I have the same issue with this regex, /(\s|^)(https?:\/\/[^\s]+)(?<!\.)/. this 's used to check a string contains url format. Could you have a look for meAmory
can we say that the non-capturing group works exactly the same as the lookbehind?Undistinguished
@Undistinguished They serve different purposes, but you can usually refactor a pattern that includes lookbehind to one that uses a different approach to achieve the same outcome. A non-capturing group is just a logical group that you don't want to use the result of (in a backreference or in the result of the regex).Alti
@Prase It's not the new Internet Explorer because it's always been trash. Doesn't need to supercede IE since it was terrible back in the day as well.Shrievalty
B
5

Regex ?<= not supported Safari iOS, we can use ?: Note: / or 1st reference letter that comes before in a non-captured group

See detail: https://caniuse.com/js-regexp-lookbehind

let str = "Get from Slash/to Next hashtag #GMK"


let workFineOnChromeOnly = str?.match(/(?<=\/)([^#]+)(?=#*)/g)
console.log("❌ Work Fine On Chrome Only", workFineOnChromeOnly )


let workFineSafariToo = str?.match(/(?:\/)([^#]+)(?=#*)/g)
console.log("✔️ Work Fine Safari too", workFineSafariToo )
Ballottement answered 3/1, 2023 at 13:49 Comment(0)
A
5

Safari added the lookbehind support in 16.4.

https://developer.apple.com/documentation/safari-release-notes/safari-16_4-release-notes#JavaScript

Archival answered 14/4, 2023 at 8:49 Comment(2)
For the record, I still ran into this in 16.6, weirdly.Okie
Thank you! Updating the browser solved the problem.Deidredeific
B
4

The support for RegExp look behind assertions as been issued by web kit:

Check link: https://github.com/WebKit/WebKit/pull/7109

Biforate answered 21/12, 2022 at 13:57 Comment(0)
P
3

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

Plenipotentiary answered 1/6, 2022 at 2:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.