12

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!

Matchu
  • 83,922
  • 18
  • 153
  • 160
bear
  • 1,318
  • 5
  • 16
  • 26
  • possible duplicate of [Jquery Extract URL from Text](http://stackoverflow.com/questions/4189124/jquery-extract-url-from-text) – miku Dec 21 '10 at 23:18
  • This has more to do with Javascript the language than jQuery the library. – Matchu Dec 21 '10 at 23:19
  • I also asked a similar question: http://stackoverflow.com/questions/4308732/regex-replace-url-with-links – Mottie Dec 21 '10 at 23:22
  • fudgey's is way over my head. I get it but I'm still blocking on applying it... – bear Dec 21 '10 at 23:30

4 Answers4

30

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.

Simon Williams
  • 309
  • 3
  • 3
7

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 →

mVChr
  • 49,587
  • 11
  • 107
  • 104
  • really bad answer. Never, _ever_, _ever_ attempt to guess what a legal domain-name is using a regexp. – Alnitak Apr 02 '13 at 16:12
1

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/.

Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232
0

Try this to get all type of links

http://aaa.aaa.aaa

www.aaa.aaa

www.aaa.aaa/aaa

string.match(/(www|http|https|ftp|ftps)?:?/?/?[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(/\S*)?/gi);