0

Is there any regx to detect complete url

In my code i am using this regx urlRegex to find out any url in my content editable iframe's text

urlRegex = /http(s?):\/\/($|[^ ]+)/

iam getting issue when i type http:// this gets detecting and this is not detecting www.google.com

i have to detect url's like

http://www.google.com
https://www.google.com
www.google.com
http://localhost:51932/default.aspx

my code is

var iframe = Iframe,
    textbox = iframe.contents().find("body")[0],
    urlRegex = /http(s?):\/\/($|[^ ]+)/;

In this question Autolink URL in ContentEditable Iframe i am trying to fix regx

Please help

Thanks

Community
  • 1
  • 1
dhee
  • 467
  • 1
  • 4
  • 14

1 Answers1

1

Updated version - tested

^((https?|s?ftp):\/\/)?(((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])))(:[1-9][0-9]+)?(\/)?([/?].+)?$

Only two fails (for now :))

http://localhost:51932/default.aspx
https://192.168.0.25:5132/default.aspx
www.google.com
http://www.google.com
mailto:somebody@google.com                         // fail
somebody@google.com                                // fail 
www.url-with-querystring.com/?url=has-querystring
http://www.xxx.events
sftp://ftp.something.com
www.google.co.in
www.xxx.com
hex494D49
  • 9,109
  • 3
  • 38
  • 47