0

This is what I am trying to achieve.

  1. User should not enter file protocol including www So http(s)://www. should fail
  2. URL can have sub domains and file extensions
  3. URL can have multiple levels like user/john/form.html

Valid scenarios

 1. All urls expcept protocol and www Eg: https://www. , http://www 

Which means all urls should be accepted but without protocol and www

I tried these regex

  1. ^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$

  2. /^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$/gm

  3. /^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/

Demo here

let re = /^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/;
let url = 'xxxx.xxxx.xxxx'; // this is valid but test returns false
let url2 = 'https://www.xxzx.com/xxx.aspx'; // this should fail as there is https://www in url

console.log(re.test(url)); //
console.log(re.test(url2)); //
phantomCoder
  • 1,499
  • 3
  • 18
  • 32
  • URLs can contain many more characters than `[a-z0-9]`, attempting to do this with a regex is largely doomed to failure. You're better off using a solution like this: https://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript – user229044 May 10 '19 at 03:19
  • @meagar I am fine with any characters, but just dont want protocol and www. I will update question. Kindly reopen. – phantomCoder May 10 '19 at 03:32

0 Answers0