179

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

Invalid regular expression: invalid group specifier name

Any ideas?

14
  • 4
    It seems that Safari is not yet compliant with the 2018 standard. There's a bug report here that is over 4 years old! Commented Oct 20, 2021 at 6:49
  • 63
    Year 2022. Still an issue with Safari Commented Apr 25, 2022 at 18:49
  • 5
    sometimes I wonder what goes on at apple
    – rednoyz
    Commented Oct 13, 2022 at 7:13
  • 5
    Hey, I'm from future, 2167 outside, still an issue
    – maveriq
    Commented Oct 29, 2022 at 14:38
  • 20
    Year 2023. Still an Issue with Safari, lol.
    – telion
    Commented Jan 5, 2023 at 16:17

5 Answers 5

236

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.

11
  • 1
    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! Commented Dec 3, 2019 at 10:56
  • 1
    can you please help me to make this compatible with safari. /\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g Commented Apr 26, 2021 at 13:23
  • 2
    @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. Commented Jan 20, 2022 at 13:55
  • 18
    Adding this to the long, long list of things to throw at people who claim that Safari is not the new Internet Explorer.
    – csvan
    Commented Apr 12, 2022 at 14:00
  • 1
    @kayochin 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). Commented Nov 21, 2022 at 13:54
6

Safari added the lookbehind support in 16.4.

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

2
  • 1
    For the record, I still ran into this in 16.6, weirdly.
    – Regular Jo
    Commented Aug 12, 2023 at 21:55
  • Thank you! Updating the browser solved the problem.
    – MrEduar
    Commented Oct 3, 2023 at 17:17
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 )

4

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

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.