1

I have the following regex to detect URLS:

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

However, it doesn't detect urls such as www.google.ca and tlk.tc/ApSE. Is there an regex where I can detect these URLs? I am using javascript.

3
  • 1
    Try this link (google: javascript url regex).
    – maerics
    Jan 17, 2012 at 16:37
  • Important ftp connections can also be secure (ftps, sftp) and file: require three slashes file:/// on windows
    – noob
    Jan 17, 2012 at 16:56
  • "www.google.ca" is not a URL, it's an FQDN.
    – johnsyweb
    Feb 1, 2012 at 20:25

2 Answers 2

3

Edit:

Try this one:

((\b(https?|ftp|file):\/\/)?[-A-Z0-9+&@#\/%?=~_|!:,.;]+\.[-A-Z0-9+&@#\/%=~_|]+)

It makes the scheme optional, to support the two cases that you show in your example.

The IETF RFC-2396 for URLs gives the following regular expression for parsing URLs:

^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?

which maps the capture groups to the following components:

scheme    = $2
authority = $4
path      = $5
query     = $7
fragment  = $9

Note that the examples you give, www.google.ca and tlk.tc/ApSE are not "valid" URLs, but I believe they'd be matched by the regex anyway.

4
  • I have tested this expression here: gskinner.com/RegExr and it doesn't work with any domain o.0
    – Armin
    Jan 17, 2012 at 17:02
  • You're right... I misread the IETF doc. This regex is used for parsing URLs, not finding them. I'll update my answer.
    – Jonathan
    Jan 17, 2012 at 17:05
  • Try ^(([^:/?\#]+):)?(//([^/?\#]*))?([^\?\#]*)(\?([^\#]*))?(\#(.*))? at that website.
    – PP.
    Jan 17, 2012 at 17:09
  • I am trying to match the url shortener like twitter so I want to detect URLs like twitter.
    – Nitrodbz
    Jan 30, 2012 at 20:19
0

This expression does what you want. It is not a valid URL which this regexp is matching, but it fits your requirements:

/(\b(https?|ftp|file):\/\/|\bwww\.[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])|([\S]+\.([a-z]{2,})+?\/[\S]+)/gi
7
  • this works except for google.ca where it counts it as two urls (according to a piece of code I've written). Why does it do this?
    – Nitrodbz
    Jan 31, 2012 at 18:23
  • It requires a slash after the domain. Your requirement was: www.google.ca and tlk.tc/ApSE. Either the www. or the slash and at least one char is missing.
    – Armin
    Jan 31, 2012 at 22:57
  • How would you change the expression so that once it detects http:// and then www comes right after, then that's one url instead of separating them into two?
    – Nitrodbz
    Feb 2, 2012 at 16:33
  • Just move the first closing bracket (after |file) behind www\.. So the whole regex looks like this: (\b(https?|ftp|file:\/\/|\bwww\.)[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])|([\S]+\.([a-z]{2,})+?\/[\S]+)
    – Armin
    Feb 3, 2012 at 9:44
  • And, was my regular expression helpful for you?
    – Armin
    Feb 7, 2012 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.