Alex
Alex

Reputation: 6665

Regex to validate a twitter url

I'm using the jQuery validation engine to parse my form data: https://github.com/posabsolute/jQuery-Validation-Engine

What would be the regex to validate a twitter URL (including the http://twitter.com part) Eg:

http://twitter.com/barackobama
or
http://twitter.com/#!/barackobama

Upvotes: 13

Views: 21574

Answers (6)

SLaks
SLaks

Reputation: 887867

You mean

/http(?:s)?:\/\/(?:www\.)?twitter\.com\/([a-zA-Z0-9_]+)/

Upvotes: 14

Amish Shabani
Amish Shabani

Reputation: 759

You should consider differences between http and https and of course www. Many of answer forget this rule.

'/^(?:https?:\/\/)?(?:www\.)?twitter\.com\/(#!\/)?[a-zA-Z0-9_]+$/i'

Upvotes: 2

morgan9999
morgan9999

Reputation: 801

/(?:http:\/\/)?(?:www\.)?twitter\.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*([\w\-]*)/

with optional using https or add www e.g

http://twitter.com/example
https://twitter.com/example
http://www.twitter.com/example
https://www.twitter.com/example

is valid

Upvotes: 15

Beright
Beright

Reputation: 395

I'm not familiar with jQuery but if it uses standard regex it would be something like this:

http://twitter.com/(#!/)?[a-zA-Z0-9_]{1,15}

This bit (#!/)? is to make the #!/ optional, and this [a-zA-Z0-9_]{1,15} is because Twitter usernames can contain letters (upper or lowercase), numbers and underscores and can be up to 15 characters in length.

Upvotes: 2

mVChr
mVChr

Reputation: 50205

url.match(/http:\/\/twitter.com\/(#!\/)?(\w*)/i);

Upvotes: 0

bluepnume
bluepnume

Reputation: 17128

How about:

http:\/\/twitter\.com\/(#!\/)?\w+

Upvotes: 0

Related Questions