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!

4
  • possible duplicate of Jquery Extract URL from Text Commented Dec 21, 2010 at 23:18
  • This has more to do with Javascript the language than jQuery the library. Commented Dec 21, 2010 at 23:19
  • I also asked a similar question: stackoverflow.com/questions/4308732/… Commented Dec 21, 2010 at 23:22
  • fudgey's is way over my head. I get it but I'm still blocking on applying it... Commented Dec 21, 2010 at 23:30

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

really bad answer. Never, ever, ever attempt to guess what a legal domain-name is using a regexp.
@Alnitak which is your proposal?
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 →

1 Comment

really bad answer. Never, ever, ever attempt to guess what a legal domain-name is using a regexp.
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/.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.