0

I have a problem with a regular expression, I tested that in some websites and it is working but when I use that regular expression in the project it is not matching with the correct result. the regular expression is the following

^(http|https)\://(www\.)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$

I need that kind of regular expression because I need validate something like that:

http://www.test.com
https://www.test.com/login

the code that I am using is the following

var pattern = new RegExp(URL_REGEXP);
if (pattern.test($('input.editValueText').val()))
j08691
  • 204,283
  • 31
  • 260
  • 272
rjimenez
  • 58
  • 1
  • 5
  • 1
    possible duplicate of [Detect URLs in text with JavaScript](http://stackoverflow.com/questions/1500260/detect-urls-in-text-with-javascript) – pdw Jul 21 '14 at 16:44
  • http://regex101.com/r/oM6yW8/1 <-- shows your problem. – mechalynx Jul 21 '14 at 16:54

1 Answers1

2

You forget to add + after the last character class. Your regex would be,

^(http|https):\/\/(www\.)[a-zA-Z0-9-.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9])?\/?([a-zA-Z0-9-._\?\,\'\/\+&%\$#\=~]+)*$

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • it is the answer, thanks bro. I spent more than 1 hour searching what is wrong. Thanks a lot for your answer – rjimenez Jul 21 '14 at 16:52