R
Reactiflux

✅ – Hailwood – 00-51 Oct 7

✅ – Hailwood – 00-51 Oct 7

HHailwood10/7/2022
Can someone please help me with some regex - Given strings like
"this definitely will not match"
"this min-h-32 should not match"
"this h-12 should match"
"this h-12-no not match"
"this definitely will not match"
"this min-h-32 should not match"
"this h-12 should match"
"this h-12-no not match"
I want to match if the string contains h-[numbers] The h-[numbers] can appear anywhere in the string (end,start,middle) but cannot be "touching" any other characters other that whitespace.
RRhys10/7/2022
@Hailwood something like?
(?<=\s|^)h-(\d*)(?!\S)
(?<=\s|^)h-(\d*)(?!\S)
HHailwood10/7/2022
@Rhys it looks good and works well in chrome etc.. any idea why in safari I'm getting
"Invalid regular expression: invalid group specifier name"?
'hello h-12 lol'.match(/(?<=\s|^)h-(\d*)(?!\S)/gm)
'hello h-12 lol'.match(/(?<=\s|^)h-(\d*)(?!\S)/gm)
Oh...https://caniuse.com/js-regexp-lookbehind Hmm, any thoughts on doing it without a lookbehind?
RRhys10/7/2022
I'll do some tests and let you know @Hailwood , should be possible
RRhys10/7/2022
@Hailwood here you go, I actually can explain this one haha (^|\ )h-(\d*)($|\ )
RRhys10/7/2022
(^|\ ) - matches start of string or whitespace h- matches h- (\d*)- matches digits as a capture group ($\ )-matches end of string or whitespace
HHailwood10/7/2022
Ahaha that's much simplier than I had in my head. Nice. Cheers!
RRhys10/7/2022
👍 you can use a ✅ reaction on the solution to mark this as solved 🙂
UUUnknown User10/7/2022
3 Messages Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

✅ – Hailwood – 00-51 Oct 7

Join Server