-1

I have this regex:

(https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})

which works fine except it accepts a URL like:

http://55https//www.google.com/

How can I make it not allow something like

55https//

after

http://

has already been entered? (or before www or the actual start of the domain name).

The regex accepts these as well:

www.demo.com
~http://foo.co.uk/
Pmpr.ir
  • 16,260
  • 23
  • 83
  • 97
jubilantdollop
  • 223
  • 2
  • 11
  • Possible duplicate of [What is the best regular expression to check if a string is a valid URL?](http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) – Roberto Linares Jun 24 '16 at 17:52

1 Answers1

1

Use ^ to anchor your regex to the start of the string, and/or $ to anchor it to the end.

(^https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,}$)
Blazemonger
  • 86,267
  • 25
  • 136
  • 177