171

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

Invalid regular expression: invalid group specifier name

Any ideas?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
techguy2000
  • 4,861
  • 6
  • 32
  • 48

5 Answers5

228

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.

Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Thanks for this. I need to get the index 1 from the exec result rather than index 0. But it works. – techguy2000 Jul 28 '18 at 07:06
  • 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! – Nuno Ribeiro Dec 03 '19 at 10:56
  • 1
    can you please help me to make this compatible with safari. /\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g – TheEhsanSarshar Apr 26 '21 at 13:23
  • I think this is a similar issue, can you please help? https://stackoverflow.com/questions/68273961/regex-lookbehond-not-supported-in-safari-how-to-achieve-the-same – Pikk Jul 06 '21 at 17:49
  • 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). – Aram Becker Jan 20 '22 at 12:08
  • 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. – CertainPerformance Jan 20 '22 at 13:55
  • 14
    Adding this to the long, long list of things to throw at people who claim that Safari is not the new Internet Explorer. – csvan Apr 12 '22 at 14:00
  • @techguy2000 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 me – Ming Hieu Jul 14 '22 at 11:50
  • can we say that the non-capturing group works exactly the same as the lookbehind? – kayochin Nov 21 '22 at 11:56
  • 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). – CertainPerformance Nov 21 '22 at 13:54
  • @csvan 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. – Matt Kenefick Aug 17 '23 at 20:16
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 )
GMKHussain
  • 3,342
  • 1
  • 21
  • 19
4

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

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

JBB
  • 51
  • 2
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).

Soviut
  • 88,194
  • 49
  • 192
  • 260
2

Safari added the lookbehind support in 16.4.

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

Tonni
  • 94
  • 2
  • 7