I would like to match space characters () only if they are followed by a hash (#).
This is what ( #) below is trying to do, which is a capture group. (I tried escaping the brackets, otherwise the brackets are not recognised properly within a group set). However, this is not working.
The below regex
/#[a-zA-Z\( #\)]+/g
matches all of the below
#CincoDeMayo #Derby party with UNLIMITED #seafood towers
while I would like to match #CincoDeMayo #Derby and separately #seafood
Is there any way to specify captures groups () within a character set []?
Character classes are meant to match a single character, thus, it is not possible to define a character sequence inside a character class.
I think you want to match specific consecutive hashtags. Use
/#[a-zA-Z]+(?: +#[a-zA-Z]+)*/g
or
/#[a-zA-Z]+(?:\s+#[a-zA-Z]+)*/g
See the regex demo.
Details
#[a-zA-Z]+ - a # followed with 1+ ASCII letters(?: - start of a non-capturing group...
\s+ - 1+ whitespaces#[a-zA-Z]+ - a # followed with 1+ ASCII letters)* - ... that repeats 0 or more times.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With