SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Find URLs within string - return as array

    Hi,

    I have a string like this:

    Code:
    hello, blah blah http://www.google.com blah blah blah http://www.google2.com blah blah
    Somehow I need to produce an array with nested arrays each containing the URLs and their respective indexes within the string.

    Code JavaScript:
    // Generated automatically:
    var myNewArray = [
        [ 'http://www.google.com' , 17 ],
        [ 'http://www.google2.com' , 54 ]
    ]

    I am hoping for a self-contained function which can be passed a string and will return something similar to the above array.

    I thought I had it all worked out but just got totally lost in the midst of RegExp, indexOf etc. so I turned to Sitepoint.. Hopefully someone here can help!

    All help appreciated, thanks!
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the matching string and its index to define each array item as a two member array.
    In the returned array, A[0][0] is the first url, A[0][1]is the indexOf position of the first url.

    Code:
    String.prototype.indexMatch= function(Rx){
    	var s= this, A= [], M, z,i;
    	while((M= Rx.exec(s)) != null){
    		i= M.index;
    		z= Rx.lastIndex;
    		A[A.length]= [s.substring(i, z),i];
    	}
    	return  A;
    }
    var s='In 1960 I saw 20 11 year olds and 11 20 year olds.';
    var rx=/\d+/g
    var M= s.indexMatch(rx);
    alert(M.join('\n'))


    The regular expression is up to you- depends on what you want to call an url.


    var s=document.body.innerHTML;
    var M= s.indexMatch(/(http\:[\S]+)/g);
    alert(M.join('\n'));
    Last edited by mrhoo; Oct 24, 2008 at 21:52.

  3. #3
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works perfectly thanks!!!

    Just FYI for others:

    Code JavaScript:
    var v = $('#status').val();
    var vi = v.indexOf('http://');
    if(v&&vi>-1) {
        var urls = (function(s,Rx){
            var A = [], M, z = 0;
            while((M= Rx.exec(s)) != null){
                i= M.index;
                z= Rx.lastIndex;
                A[A.length]= [s.substring(i, z),i];
            }
            return A;
        })(v,/(http\:[\S]+)/g);
        unsafeWindow.console.log(urls);
    }
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    I would like to try and make that code more easily understandable.

    Code JavaScript:
    var statusValue = $('#status').val();
    var urlIndex = statusValue.indexOf('http://');
    if(urlIndex > -1) {
        var urls = findAsArrayWithIndex(statusValue, /(http\:[\S]+)/g);
    }
    // do something with urls
     
    function findAsArrayWithIndex(text, regEx) {
        var urls = [],
            match = regEx.exec(text);
            start,
            end;
        while(match !== null) {
            start = match.index;
            end = regEx.lastIndex;
            urls.push([text.substring(start, end), start]);
            match = regEx.exec(text);
        }
        return urls;
    }

    And you can get some compatibility code from http://developer.mozilla.org/en/Core...cts/Array/push to make the array.push method compatible with older IE browsers too.

    Is that any easier to understand?
    Last edited by paul_wilkins; Oct 26, 2008 at 14:45.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, thanks Paul!

    Luckily, I'm developing a GreaseMonkey script so I only need to worry about FF.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •