Matching @ as first character
I am creating two regular expression helpers.
The first replaces any links to the anchor tag. This is how it looks:
String.prototype.parseURL = function() {
return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=@]+/g, function(url) {
return url.link(url);
});
};
The second place replaces any tweeters (starting with @
) with an anchor tag that points to the corresponding Twitter profile. This is what it currently looks like:
String.prototype.parseUsername = function() {
return this.replace(/\s[@]+[A-Za-z0-9-_]+/g, function(u) {
var username = u.replace("@","")
return u.link("http://twitter.com/"+username);
});
};
Both of these prototype methods are then chained, which replaces the correctly matched inputs. The previous edge case that I captured included a character @
in a hyperlink.
There is one extreme case that I don't encounter when the Twitter handle is at the beginning of a line (no characters before it, no spaces, etc.).
How can I parseUsername
match any instances @
that do not have any characters like slashes / tags / hyphens / etc. before it, but are the first instance in the first word of the line?
Here's a picture of what's going on:
source to share