The task is the following - you need to create a regular expression that checks the URL, and if it has the format https://vk.com/[0-9A-z._] , but NOT https://vk.com/album[0-9] give the desired result.

 var value = document.getElementById('name').value; var regex = /^https:\/\/vk.com\/^album\d(\d\w){1,100}+$/; if( regex.test(value) ) alert('ok'); else alert('fail'); 

does not work. tell me what I'm doing wrong?

2 answers 2

Use

 /^https:\/\/vk\.com\/(?!album-?\d)[^\/]*$/ 

See the regular expression demo

Details

  • ^ - beginning of line
  • https:\/\/vk\.com\/ - substring https://vk.com/
  • (?!album-?\d) - immediately after the current position in the line there should not be substrings such as album-1 or album1
  • [^\/]* - 0 and more than any characters other than /
  • $ - end of line.
     /^https:\/\/vk\.com\/(?!album-?\d+$)[\w.]+$/ 
    • Thank you. It works, if there is a dash after the album, if there is no dash, it is still being tested .. - Sergey Belov
    • @ Sergey Belov, where? i.stack.imgur.com/z68Dd.png - Qwertiy pm
    • parser2.ru/test.html - Sergey Belov
    • @ Sergey Belov, and? Still works. - Qwertiy
    • VK albums can have links like vk.com/album123_456 or vk.com/album-123_456 (with and without dashes), the first option returns ok, the second fail .. - Sergey Belov