I have this beautiful function, but I need to customize it to return just an array of items matching the regex. so the result would be #hash1234, #sweetthing,#something_notimportant Is there any way to do this using this function?
String.prototype.parseHashtag = function() {
return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {
var tag = t.replace("#", "%23");
return t.link("http://search.twitter.com/search?q=" + tag);
});
};
var string = '#hash1234 this is another hash: #sweetthing and yet another #something_notimportant';
$('#result').html(string.parseHashtag());
The
.matchmethod returns an array of all matches, ornullif there were no matches.So if
nullis an acceptable return for the no-match situation then:Or if you’d prefer to return an empty array or other default for no-match: