w...">

Javascript: find urls in a document

how to find urls (e.g. www.domain.com) in a document and put them in anchors: <a href = "www.domain.com"> www.domain.com </a>

HTML:

Hey dude, check out this link www.google.com and www.yahoo.com!

JavaScript:

(function(){var text = document.body.innerHTML;/*do replace regex => text*/})();

output:

Hey dude, check out this link <a href="www.google.com">www.google.com</a> and <a href="www.yahoo.com">www.yahoo.com</a>!
+5
source share
2 answers

Firstly, www.domain.comit is not a URL, it is the host name and

<a href="www.domain.com">

will not work - it searches for a file .comwith a name www.domainrelative to the current page.

, . "www.something.dot.separated.words", , , www. hostname. .

/\bhttps?:\/\/[^\s<>"`{}|\^\[\]\\]+/;

, URL- HTTP. , , , , , . !, URL-, , , .

( |, URL www.hostname, .)

, , . innerHTML.. , href="http://something", . JavaScript, innerHTML.

regexp HTML . , HTML ​​ . <a>, URL- , , ( ).

// Mark up `http://...` text in an element and its descendants as links.
//
function addLinks(element) {
    var urlpattern= /\bhttps?:\/\/[^\s<>"`{}|\^\[\]\\]+/g;
    findTextExceptInLinks(element, urlpattern, function(node, match) {
        node.splitText(match.index+match[0].length);
        var a= document.createElement('a');
        a.href= match[0];
        a.appendChild(node.splitText(match.index));
        node.parentNode.insertBefore(a, node.nextSibling);
    });
}

// Find text in descendents of an element, in reverse document order
// pattern must be a regexp with global flag
//
function findTextExceptInLinks(element, pattern, callback) {
    for (var childi= element.childNodes.length; childi-->0;) {
        var child= element.childNodes[childi];
        if (child.nodeType===Node.ELEMENT_NODE) {
            if (child.tagName.toLowerCase()!=='a')
                findTextExceptInLinks(child, pattern, callback);
        } else if (child.nodeType===Node.TEXT_NODE) {
            var matches= [];
            var match;
            while (match= pattern.exec(child.data))
                matches.push(match);
            for (var i= matches.length; i-->0;)
                callback.call(window, child, matches[i]);
        }
    }
}
+6

All Articles