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!
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.
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 →
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/.
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);
精彩评论