Reputation: 3878
I'd like some help on figuring out the JS regex to use to identify "hashtags", where they should match all of the following:
#foobar
abc123#xyz456
#[foo bar]
(that is, the [] serves as delimiter for the hashtag)For 1 and 2, I was using something of the following form:
var all_re =/\S*#\S+/gi;
I can't seem to figure out how to extend it to 3. I'm not good at regexps, some help please?
Thanks!
Upvotes: 8
Views: 15528
Reputation: 1083
I had a similar problem, but only want to match when a string starts and ends with the hashtag. So similar problem, hopefully someone else can have use of my solution.
This one matches "#myhashtag" but not "gfd#myhashtag" or "#myhashtag ".
/^#\S+$/
^ #start of regex
\S #Any char that is not a white space
+ #Any number of said char
$ #End of string
Simple as that.
Upvotes: 0
Reputation: 816442
So it has to match either all non-space characters or any characters between (and including) [
and ]
:
\S*#(?:\[[^\]]+\]|\S+)
Explanation:
\S* # any number of non-white space characters
# # matches #
(?: # start non-capturing group
\[ # matches [
[^\]]+ # any character but ], one or more
\] # matches ]
| # OR
\S+ # one or more non-white space characters
) # end non-capturing group
Reference: alternation, negated character classes.
Upvotes: 31