Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ).

like image 478
ppp Avatar asked Dec 27 '11 22:12

ppp


People also ask

What is the format of a Twitter username?

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. The username may be claimed by a suspended or deactivated account.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

Can you use regex in Twitter search?

Twitter unfortunately doesn't support searching of tweets using regular expressions which means that you do have to post process. There's not actually any official documentation from Twitter to that effect, but everyone who uses the Twitter search API post-processes their tweets using regex (including me).


1 Answers

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

like image 180
asenovm Avatar answered Oct 04 '22 11:10

asenovm