Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regex for matching twitter-like hashtags

I'd like some help on figuring out the JS regex to use to identify "hashtags", where they should match all of the following:

  1. The usual twitter style hashtags: #foobar
  2. Hashtags with text preceding: abc123#xyz456
  3. Hashtags with space in them, which are denoted as: #[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!

like image 417
ragebiswas Avatar asked Aug 07 '12 13:08

ragebiswas


2 Answers

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.

like image 50
Felix Kling Avatar answered Nov 14 '22 13:11

Felix Kling


How about this?

var all_re =/(\S*#\[[^\]]+\])|(\S*#\S+)/gi;
like image 32
xiaowl Avatar answered Nov 14 '22 13:11

xiaowl