Regular expression for twitter username

I need a javascript regular expression to match twitter usernames.

The username is entered by the user while signing up, so I don't want to distract them with too many error notifications. Because of that, I need the expression to match valid usernames regardles if they have the @ before the username or not.

Twitter usernames can contain latin characters, underscores and numbers, and the only limitation is the can be up to 15 characters long. ( but I need the regex to match 16 characters as well, in case someone enters the @ before the username ).

Answers


This should do: ^@?(\w){1,15}$


This is the best solution I found yet to replace multiple occurrences of a twitter username.

The regex doing the trick is /(^|[^@\w])@(\w{1,15})\b/. I am catching what stand behind the @ character so I can replace the username correctly. And I am using global match flag (g) so it will replace all occurrences. asenovm answer is simple, but will not work in most user input contexts, as techexpert is explaining in his comment.

var output,
    text    = "@RayFranco is answering to @AnPel, this is a real '@username83' but this is an@email.com, and this is a @probablyfaketwitterusername",
    regex   = /(^|[^@\w])@(\w{1,15})\b/g,
    replace = '$1<a href="http://twitter.com/$2">@$2</a>';

output = text.replace( regex, replace );

console.log ( output );

This is giving me what I expected (tested with node v0.9.1) :

@RayFranco is answering to @AnPel, this is a real '@username83' but this is an@email.com, and this is a @probablyfaketwitterusername

This is based on Twitter "specs" for username :

Your username cannot be longer than 15 characters. Your real name can be longer (20 characters), but usernames are kept shorter for the sake of ease. A username can only contain alphanumeric characters (letters A-Z, numbers 0-9) with the exception of underscores, as noted above. Check to make sure your desired username doesn't contain any symbols, dashes, or spaces.

Hope this helps.


A short an easy way to do it:

function validTwitteUser(sn) {
    return /^[a-zA-Z0-9_]{1,15}$/.test(sn);
}

It may be more than you need but I found this in another post "regex how to replace twitter links". Wraps @usernames, #hashtags and urls. Working well for me.

function processTweetLinks(text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
    text = text.replace(exp, "<a href='$1' target='_blank'>$1</a>");
    exp = /(^|\s)#(\w+)/g;
    text = text.replace(exp, "$1<a href='https://twitter.com/hashtag/$2?src=hash' target='_blank'>#$2</a>");
    exp = /(^|\s)@(\w+)/g;
    text = text.replace(exp, "$1<a href='http://www.twitter.com/$2' target='_blank'>@$2</a>");
    return text;
}

@[a-zA-Z0-9_]{0,15}

You can use above regular expression to sort of the twitter usernames from a mixed set of data


To exclude "non-latin" characters, you have to use: ^@?([a-zA-Z0-9_]){1,15}$. Because, \w accepts "any word characters". And non-latin characters qualifies this condition. So, it matches even ΓΌΓΆ like Turkish characters as well.


Need Your Help

Disable selection of a single UITableViewCell

ios iphone cocoa-touch uitableview

How do you disable selecting only a single cell in a UITableView? I have several, and I only want the last to be disabled.

Attach File Through mailto URI

javascript email attachment mailto

Is it possible to attach a file using "mailto:" redirect in javascript?