0

I'm writing a RegEx for domain name validation that shouldn't allow http:// or www at the start (i.e. msft.com, google.com,amazon.com, etc.). I've found plenty of posts on this site and the closest came with this solution:

^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)+[A-Za-z]{2,6}$

This won't allow http:// but it will allow me to enter www.google.com. How can I adjust this?

Thanks for any helpful tips.

fumeng
  • 1,771
  • 5
  • 22
  • 61
  • 1
    Just an FYI the [lookbehind](http://www.regular-expressions.info/lookaround.html) regex is not supported in JavaScript so I'm not sure if the `<!-` will work. – aug Oct 29 '15 at 18:09
  • You're exactly right. It's not working with JS. – fumeng Oct 29 '15 at 18:19
  • 1
    Might be a useful take a look: http://stackoverflow.com/questions/7376238/javascript-regex-look-behind-alternative – aug Oct 29 '15 at 20:44

1 Answers1

4

Replace the ^ with ^(?!www\.):

^(?!www\.)((?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)+[A-Za-z]{2,6}$

This just means that the start of the string (^) cannot be followed by www..

elixenide
  • 44,308
  • 16
  • 74
  • 100