How do I extend this regex to match www.example.com in addition to http://example.com?

Posted by algorithmicCoder on Stack Overflow See other posts from Stack Overflow or by algorithmicCoder
Published on 2013-10-23T03:33:51Z Indexed on 2013/10/23 3:53 UTC
Read the original article Hit count: 175

Filed under:
|

I've got a regex for detecting links that works pretty well for urls of the form http://example.com, http://www.example.com etc but not for www.example.com

I am trying to extend what i have so that www.foo.com also gets matched without breaking previous functionality.

I tried this:

/\(?\b((http|https|ftp):\/\/|)(www\.)[-A-Za-z0-9+&@#\/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#\/%=~_()|]/

but this only works when www is present and doesn't work e.g. for http://example.com ...

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex