In my Javascript code, this regex /(?<=\/)([^#]+)(?=#*)/
works fine in Chrome, but in safari, I get:
Invalid regular expression: invalid group specifier name
Any ideas?
In my Javascript code, this regex /(?<=\/)([^#]+)(?=#*)/
works fine in Chrome, but in safari, I get:
Invalid regular expression: invalid group specifier name
Any ideas?
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.
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 )
Safari added the lookbehind support in 16.4.
https://developer.apple.com/documentation/safari-release-notes/safari-16_4-release-notes#JavaScript
The support for RegExp look behind assertions as been issued by web kit:
Check link: https://github.com/WebKit/WebKit/pull/7109
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).
© 2022 - 2024 — McMap. All rights reserved.