0

I am trying to validate a textbox to ensure that a URL is written inside (minus the "http://" part). Here is my code:

var isUrl;
var regex = new RegExp("^(?!http)(www\.)?(([a-z])+\.[a-z]{2,}(\.[a-z]{2,})?)");
var siteUrl = e.target.value;
if (siteUrl.match(regex)) {
   isUrl = true;
}
else {
   isUrl = false;
}

So my regular expression is ^(?!http)(www\.)?(([a-z])+\.[a-z]{2,}(\.[a-z]{2,})?). I was under the impression that this would do the following:

  1. NOT match anything beginning with http.. which it does correctly
  2. Allow an optional www. at the start
  3. Accept 1 or more characters from a-z to be typed in
  4. Accept a dot after those characters
  5. Accept 2 or more characters following the dot, and
  6. Allow an optional dot followed by two or more characters again.

In practice, the textbox accepts strings like "ashdiguasdf" and "aksjdnfa:://',~" which it should not do. Can anyone explain why?

user2412643
  • 153
  • 1
  • 1
  • 13
  • 1
    Use a regular expression literal or double escape using a constructor. Also, you don't need the Negative Lookahead if you're anchoring your pattern, you can omit that part and make sure you use the end of string `$` anchor as well. – hwnd Jun 24 '15 at 15:13
  • 1
    I.e. use `var regex = /^(?!http)(www\.)?(([a-z])+\.[a-z]{2,}(\.[a-z]{2,})?)/i;` (I think case-insensitive matching is necessary here, thus I added `/i`). – Wiktor Stribiżew Jun 24 '15 at 15:19
  • Is this for very specific URLs? Are URLs without digits not allowed? Check this out - URL regex is way more complicated than expected: http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url – Mike Robinson Jun 24 '15 at 15:20
  • @stribizhev that worked, thanks man – user2412643 Jun 24 '15 at 15:22

1 Answers1

2

The main problem is that you're using the \ character in a quoted string, which javascript will interpret as the start of a "control character" (such as \n for a newline, etc).

One option is to escape it by replacing \ with \\.

But the easiest solution is to use the following format...

var regex = new RegExp(/^(?!http)(www\.)?(([a-z])+\.[a-z]{2,}(\.[a-z]{2,})?)/);

This also allows you to make it case insensitive (if you wish) by using the i character at the end, like this...

var regex = new RegExp(/^(?!http)(www\.)?(([a-z])+\.[a-z]{2,}(\.[a-z]{2,})?)/i);

As an extra bit, you're using more capture groups than really necessary. Your expression could also be written like this with the same result...

^(?!http)(?:www\.)?[a-z]+(?:\.[a-z]{2,}){1,2}
freefaller
  • 19,368
  • 7
  • 57
  • 87