Mini John
Mini John

Reputation: 7941

Regex Expression for Hashtags

/(?:\s|^)(?:#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$)))(\w+)(?=\s|$)/i

This is the current expression i use. It works fine except the dash - is not allowed. I need this: #what-ever to be captured.

How can i add the dash to this expression ?

Upvotes: 0

Views: 86

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174874

Just add the pattern which was present inside the first capturing group that is \w plus - into a character class. So that it would capture a word character or a - symbol. + after the character class makes the previous token to repeat one or more times.

(?:\s|^)(?:#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$)))([-\w]+)(?=\s|$)
                                             |here|

DEMO

Upvotes: 1

Related Questions