✅ – Hailwood – 00-51 Oct 7

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.
R
Rhys559d ago
@Hailwood something like?
(?<=\s|^)h-(\d*)(?!\S)
(?<=\s|^)h-(\d*)(?!\S)
R
Rhys559d ago
Stack Overflow
Regex: Specify "space or start of string" and "space or end of string"
Imagine you are trying to pattern match "stackoverflow". You want the following: this is stackoverflow and it rocks [MATCH] stackoverflow is the best [MATCH] i love stackoverflow [MATCH]
H
Hailwood559d ago
@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?
R
Rhys559d ago
I'll do some tests and let you know @Hailwood , should be possible
R
Rhys559d ago
@Hailwood here you go, I actually can explain this one haha (^|\ )h-(\d*)($|\ )
R
Rhys559d ago
(^|\ ) - matches start of string or whitespace h- matches h- (\d*)- matches digits as a capture group ($\ )-matches end of string or whitespace
H
Hailwood559d ago
Ahaha that's much simplier than I had in my head. Nice. Cheers!
R
Rhys559d ago
👍 you can use a ✅ reaction on the solution to mark this as solved 🙂
UU
Unknown User559d ago