20 lines
851 B
JavaScript
20 lines
851 B
JavaScript
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
/* http://daringfireball.net/2010/07/improved_regex_for_matching_urls */
|
|
const _urlRegexp = /\b(([a-z][\w-]+:(\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)([^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/gi;
|
|
|
|
// findUrls:
|
|
// @str: string to find URLs in
|
|
//
|
|
// Searches @str for URLs and returns an array of objects with %url
|
|
// properties showing the matched URL string, and %pos properties indicating
|
|
// the position within @str where the URL was found.
|
|
//
|
|
// Return value: the list of match objects, as described above
|
|
function findUrls(str) {
|
|
let res = [], match;
|
|
while ((match = _urlRegexp.exec(str)))
|
|
res.push({ url: match[0], pos: match.index });
|
|
return res;
|
|
}
|