Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract a URL from plain text using jQuery?

I'm a beginner with jQuery.

I simply want to pass a block of text to a function and return an array of urls contained within.

"I need to grab a url like http://www.something.com from text, and if therearemore.com then grab those too".

Any help? Is there a .GetUrl()?

Note: I suck with regular expressions!

like image 841
bear Avatar asked Dec 21 '10 23:12

bear


3 Answers

The jQuery Wiki Text plugin (http://www.kajabity.com/jquery-wikitext/) includes Regular Expressions to find URls in text which can be used for the purpose.

So, you asked for a function - well here it is:

/**
 * A utility function to find all URLs - FTP, HTTP(S) and Email - in a text string
 * and return them in an array.  Note, the URLs returned are exactly as found in the text.
 * 
 * @param text
 *            the text to be searched.
 * @return an array of URLs.
 */
function findUrls( text )
{
    var source = (text || '').toString();
    var urlArray = [];
    var url;
    var matchArray;

    // Regular expression to find FTP, HTTP(S) and email URLs.
    var regexToken = /(((ftp|https?):\/\/)[\-\w@:%_\+.~#?,&\/\/=]+)|((mailto:)?[_.\w-]+@([\w][\w\-]+\.)+[a-zA-Z]{2,3})/g;

    // Iterate through any URLs in the text.
    while( (matchArray = regexToken.exec( source )) !== null )
    {
        var token = matchArray[0];
        urlArray.push( token );
    }

    return urlArray;
}

Hope it helps.

like image 171
Simon Williams Avatar answered Oct 04 '22 12:10

Simon Williams


RegExp is probably the way to go, and this should do the trick for you:

var searchText = $('yourElement').text(),

    // urls will be an array of URL matches
    urls = searchText.match(/\b(http|https)?(:\/\/)?(\S*)\.(\w{2,4})\b/ig);

// you can then iterate through urls
for (var i = 0, il = urls.length; i < il; i++) {
    // do whatever with urls[i]
}

See demo →

like image 20
mVChr Avatar answered Oct 04 '22 14:10

mVChr


You will have to use a regular expression. Try:

/\i\b(http|https):\/\/(\S*)\b/i

If you aren't good with regular expression, I'd recommend taking a look at this online tool for building them: http://rubular.com/.

like image 20
Kevin Sylvestre Avatar answered Oct 04 '22 13:10

Kevin Sylvestre