I need regular expression for validating a hashtag. Each hashtag should starts with hashtag(“#”).
I have been trying with this regex
/#[a-zA-z0-9]/ but it is accepting invalid inputs also.
Any suggestions for how to do it?
To answer the current question…
There are 2 issues:
[A-z]allows more than just letter chars (
[, , ], ^, _, `)
- There is no quantifier after the character class and it only matches 1 char
Since you are validating the whole string, you also need anchors (
$)to ensure a full string match:
See the regex demo.
If you want to extract specific valid hashtags from longer texts…
This is a bonus section as a lot of people seek to extract (not validate) hashtags, so here are a couple of solutions for you. Just mind that
#char followed with one to thirty word chars followed with a word boundary
#char that is either at the start of string or right after a non-word char, then one to thirty word (i.e. letter, digit, or underscore) chars followed with one to thirty word chars followed with a word boundary
#that is either at the start of string or right after a non-word char, then one to thirty word (i.e. letter, digit, or underscore) chars (that cannot be just digits/underscores) followed with a word boundary
And last but not least, here is a Twitter hashtag regex from https://github.com/twitter/twitter-text/tree/master/js... Sorry, too long to paste in the SO post, here it is: https://gist.github.com/stribizhev/715ee1ee2dc1439ffd464d81d22f80d1.