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?

3
  • 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. Commented Jun 24, 2015 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). Commented Jun 24, 2015 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: stackoverflow.com/questions/161738/… Commented Jun 24, 2015 at 15:20

1 Answer 1

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}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.