Help create a regular javascript expression. Here is what data should be passed:

http://vk.com/user/
http://vk.com/user
vk.com/user/
vk.com/user
www.vk.com/user/
www.vk.com/user

It is clear that "user" is any text. It may also be "id123456".

  • Here is what he tried: /^(((https?)\:\/\/)?(www\.)?)?(vk\.com\/[A-Za-z0-9-‐\\/?) $ / gi Honestly admit little of this I understand. - mccrush
  • What other characters can be in the username? Underscore Cyrillic? - alexlz
  • one
    The very first brackets are superfluous - timka_s
  • Only Latin characters, numbers (from 1 to 9) and the underscore character can be in the user name. - mccrush

3 answers 3

Here is another working version:

^(https?:\/\/)?(www\.)?vk\.com\/(\w|\d)+?\/?$ 
  • then \ d is possible and not necessary. After all, \ w seems to include numbers. - mccrush
  • one
    I will slightly improve your expression ^ (?: https?: \ / \ /)? (?: www \.)? Vk \ .com \ / (. *) \ /? $ - ReinRaus
  • it’s possible not to be soared with these regulars at all, but to make an array of addresses =) - Gorets
  • at (. *) \ /? $ everything that comes after vk.com/ will fall: for example: vk.com/id1324324?get=/adsfdasf - mc-sergey
  • but in general I agree, something I have become too clever by half) then it may be better that way? > ^ (?: https?: \ / \ /)? (?: www \\.)? vk \\. com \ / [^ \ / \ s] + \ /? $ - mc-sergey
 ^(http:\/\/|https:\/\/)?(www.)?(vk\.com|vkontakte\.ru)\/(id\d|[a-zA-Z0-9_.])+$ 
  • The slash at the end, which is needed by the author, is not displayed, but, I suppose, he is extra here. Valid id currently limited to nine digits, you can specify - \d{9}) . The result: (http:\/\/|https:\/\/)?(www.)?(vk\.com|vkontakte\.ru)\/(id(\d{9})|[a-zA-Z0-9_.]+) . Demonstration - regex101.com/r/mB9aK1/1 . Thank. - Sasha Chernykh
  • Do not miss vk.com/id12345 - ReinRaus

Actually, here's the solution.

 (https{0,1}:\/\/)?(www\.)?(vk.com\/)(id\d|[a-zA-z][a-zA-Z0-9_.]{2,}) 

Since the username cannot begin with a number, for example: https://vk.com/1111111

  • instead of s{0,1} it better to write s? . - Vadim Ovchinnikov
  • a matter of taste, for me readability with braces is better in some places. - Kirill Devope