-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/
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
jubilantdollop
  • 223
  • 3
  • 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
  • 90,923
  • 26
  • 142
  • 180