Building a Hashtag in Javascript without matching Anchor Names, BBCode or Escaped Characters
- by Martindale
I would like to convert any instances of a hashtag in a String into a linked URL:
#hashtag - should have "#hashtag" linked.
This is a #hashtag - should have "#hashtag" linked.
This is a [url=http://www.mysite.com/#name]named anchor[/url] - should not be linked.
This isn't a pretty way to use quotes - should not be linked.
Here is my current code:
String.prototype.parseHashtag = function() {
return this.replace(/[^&][#]+[A-Za-z0-9-_]+(?!])/, function(t) {
var tag = t.replace("#","")
return t.link("http://www.mysite.com/tag/"+tag);
});
};
Currently, this appears to fix escaped characters (by excluding matches with the amperstand), handles named anchors, but it doesn't link the #hashtag if it's the first thing in the message, and it seems to grab include the 1-2 characters prior to the "#" in the link.
Halp!