tags:

views:

160

answers:

2

I need to find all the URLs on the page using jQuery.

They will be static URLS, by that I mean not within anchor tags.

+2  A: 

How about taking all the content of the document and parse it using a regexp?

To know if a string is a url:

function isUrl(s) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(s); }

via

marcgg
Seems like a good idea, how would I do it?
Ben Shelock
+3  A: 

If you want to find all of the literal text URLs - e.g. ones that are not part of an <a>, this should work:

var regex = /https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?/ig;
var matches = regex.exec($('body').text());
Rex M
Agreed, this is what I was saying but with actual code :)
marcgg
You need to escape all your backslashes... Alternatively, use a literal regex.
J-P
Also, you need a global flag.
J-P