Tuesday, March 1, 2011

Find All URLs on Page Using jQuery

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.

From stackoverflow
  • 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

    Ben Shelock : Seems like a good idea, how would I do it?
  • 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());
    
    marcgg : Agreed, this is what I was saying but with actual code :)
    J-P : You need to escape all your backslashes... Alternatively, use a literal regex.
    J-P : Also, you need a global flag.

0 comments:

Post a Comment