✅ – Hailwood – 00-51 Oct 7
Can someone please help me with some regex -
Given strings like
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
@Hailwood something like?
Referenced stackoverflow post: https://stackoverflow.com/questions/6713310/regex-specify-space-or-start-of-string-and-space-or-end-of-string
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]
@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"?Oh...https://caniuse.com/js-regexp-lookbehind Hmm, any thoughts on doing it without a lookbehind?
I'll do some tests and let you know @Hailwood , should be possible
@Hailwood here you go, I actually can explain this one haha
(^|\ )h-(\d*)($|\ )
(^|\ ) - matches start of string or whitespace
h- matches h-
(\d*)- matches digits as a capture group
($\ )-matches end of string or whitespace
Ahaha that's much simplier than I had in my head. Nice.
Cheers!
👍 you can use a ✅ reaction on the solution to mark this as solved 🙂
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
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