views:

28

answers:

1

I'm trying to match a set of hashtags or words in a javascript string.

(#hashtag|word)

almost does it, except I'd like to consider word boundaries.

\b(#hashtag|word)\b

doesn't match the beginning word boundary, since of course '#' is not a word character.

ideally i'd like to have something like a '\b' anchor that matches hashtags. Any idea?

+1  A: 

What about insert word boundary inside the parenths :

/(#\bhashtag|\bword)\b/
M42
`#\bh` is pointless, that's just `#h`.
polygenelubricants
nice! but...input: "a#hashtag".it matches the '#hashtag' part, but i would only like the match to occur when there is a string beginning or a space character before it.
Juan
Try this one : /(\B#hashtag|\bword)\b/
M42
that did the trick! thank you.
Juan