Hailwood
Hailwood3y ago

✅ – 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.
10 Replies
Rhys
Rhys3y ago
@Hailwood something like?
(?<=\s|^)h-(\d*)(?!\S)
(?<=\s|^)h-(\d*)(?!\S)
Rhys
Rhys3y 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]
Hailwood
Hailwood3y 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?
Rhys
Rhys3y ago
I'll do some tests and let you know @Hailwood , should be possible
Rhys
Rhys3y ago
@Hailwood here you go, I actually can explain this one haha (^|\ )h-(\d*)($|\ )
Rhys
Rhys3y ago
(^|\ ) - matches start of string or whitespace h- matches h- (\d*)- matches digits as a capture group ($\ )-matches end of string or whitespace
Hailwood
Hailwood3y ago
Ahaha that's much simplier than I had in my head. Nice. Cheers!
Rhys
Rhys3y ago
👍 you can use a ✅ reaction on the solution to mark this as solved 🙂
reactibot
reactibot3y ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1027744975561175081
reactibot
reactibot3y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1027744975561175081 Question not getting answered? Maybe it's hard to answer, or maybe you asked at a slow time. Check out these resources for help asking a good question: https://stackoverflow.com/help/how-to-ask http://wp.me/p2oIwo-26