I've looked at a couple of other possible solutions on SO but didn't see any that were doing what I was doing.
Currently I have been able to parse a string and detect hash tags with the following code:
mystring = mystring.replace(/(^|\W)(#[a-z\d][\w-]*)/ig, "$1<span class='hash_tag'>$2</span>").replace(/\s*$/, "");
And this successfully detects all sorts of #hashtags. However it also detects anchors in URLs, such as: http://www.example.com/#anchor - I can't work out how to modify what I have to exclude anchors while keeping it flexible.
Thanks
Here's a regex to match hashtag(#) if it has a space before it or it's beginning of string.. like so:
Working regex example:
http://regex101.com/r/pJ4wC5
Javascript:
Output:
The idea is to try to match the "a" tag first and after trying the hashtag subpattern that is in a capturing group. A callback function tests the capturing group and returns the "a" tag or the modifier hashtag substring:
I know this has been answered, but if you need styling, here's a solution i used on a project: