0

So I have a URL validation that I am doing that is very relaxed and more or less just guiding the user to make sure they enter close to a URL.

So I have got a regex that is doing what I want in a tester. (Only Multiline Selected) ex: http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx

However, when I call it in my code, it gives different results.

isURLValid: function (value) {
    var urlRegex = new RegExp("^((http|https|ftp)\://)*([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
    return urlRegex.test(value);
}

Requirements: Must support http, https, non http, non https, www, and non-www URL schemes

Valid

http://www.adamthings.com
https://www.adamthings.com
www.adamthings.com
adamthings.com

Invalid

http://www.adamthings 
https://www.adamthings
www.adamthings
adamthings

Results work in the tester, in code I get all of the above are valid. Why does the regex react differently?

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Adam
  • 3,615
  • 6
  • 32
  • 51
  • Check [this](http://stackoverflow.com/questions/12318023/find-and-convert-all-strings-starts-with-http-https-and-convert-into-links) – Mohan Oct 22 '12 at 15:51
  • You need to double-escape your caret: Otherwise javascript doesn't interpret the backslash as a backslash, and therefore never escapes the caret for the regex – Mohan Oct 22 '12 at 16:02
  • FWIW I always use regex planet.. http://www.regexplanet.com/advanced/javascript/index.html – Vicki Oct 22 '12 at 16:06

2 Answers2

1

In javascript: "\.com" === ".com" (I think it's not you want.)
Try /regex/ instead of new RegExp("regex") to create a regex.

kev
  • 155,172
  • 47
  • 273
  • 272
  • Doing /regex/ gives a bunch of syntax errors. I am not sure what would need escaped doing it this way, I am still getting my head fully around Regex. – Adam Oct 22 '12 at 16:11
  • @Adam - that's because you have to escape the `/` char in the regex when using the `/regex/` syntax. – jfriend00 Oct 22 '12 at 16:12
  • Using /regex/ and then escaping the / char did what I needed. – Adam Oct 22 '12 at 17:13
0

If you want to allow something like

adamthings.com

which has no leading http://, https://, ftp:// or www. part, then you would need to list all allowed TLDs, such as com, net, etc., to prevent input such as

john.doe

to be valid as well.

Then your regex pattern should look like

^(?:https?:\/\/|ftp:\/\/)?(?:www\.)?(?!www\.)(?:(?!-)[a-z\d-]*[a-z\d]\.)+(?:com|net)

which checks just domain part of the link.

If you want to go further and check trailing part or link, then you need to add

(?:\/[^\/\s]*)*$

to the first part of pattern, however this (part of regex pattern) does not validate the url.

» Test link «

Ωmega
  • 42,614
  • 34
  • 134
  • 203