Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:在文档中查找URL_Javascript_Regex_Dom_Url - Fatal编程技术网

Javascript:在文档中查找URL

Javascript:在文档中查找URL,javascript,regex,dom,url,Javascript,Regex,Dom,Url,如何在文档中查找URL(即www.domain.com)并将其放在锚定中:www.domain.com html: javascript: (function(){var text = document.body.innerHTML;/*do replace regex => text*/})(); 输出: Hey dude, check out this link <a href="www.google.com">www.google.com</a> and &

如何在文档中查找URL(即www.domain.com)并将其放在锚定中:www.domain.com

html:

javascript:

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

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>!
嘿,伙计,看看这个链接!

我从未使用过它,但这似乎是一段值得利用的代码:


首先,
www.domain.com
不是URL,而是主机名,还有

<a href="www.domain.com">
这是一个非常自由的模式,可以用作检测HTTP URL的起点。根据您所获得的输入类型,您可能希望缩小它允许的范围,并且可能值得检测尾随字符,如
这将是URL的有效部分,但实际上通常不是

(如果愿意,可以使用
|
来允许URL语法或
www.hostname
语法。)

无论如何,一旦确定了首选模式,就需要在页面的文本节点中找到该模式不要在
innerHTML
标记上运行regexp。
如果您试图标记每个
href=”,最终会完全破坏页面http://something“
这已经在标记中了。替换
innerHTML
内容时,还将销毁任何现有的JavaScript引用、事件或表单字段值

一般来说,regexp无法以任何可靠的方式处理HTML。因此,利用浏览器已经将HTML解析为元素和文本节点这一事实,只需查看文本节点即可。您还需要避免查看
元素内部,因为当URL已经位于链接中时,将其标记为链接是愚蠢的(也是无效的)

//加价`http://...`元素中的文本及其子体作为链接。
//
函数addLinks(元素){
var urlpattern=/\bhttps?:\/\/[^\s“`{}}\\\\\[\]\\]+/g;
FindTextExceptionInLink(元素、urlpattern、函数(节点、匹配){
node.splitText(match.index+match[0]。长度);
var a=document.createElement('a');
a、 href=匹配[0];
a、 appendChild(node.splitText(match.index));
node.parentNode.insertBefore(a,node.nextSibling);
});
}
//按与文档相反的顺序查找元素的后代中的文本
//模式必须是带有全局标志的regexp
//
函数findTextExceptionInLink(元素、模式、回调){
对于(var childi=element.childNodes.length;childi-->0;){
var child=element.childNodes[childi];
if(child.nodeType==Node.ELEMENT\u Node){
if(child.tagName.toLowerCase()!=='a')
FindTextExceptionLink(子级、模式、回调);
}else if(child.nodeType===Node.TEXT\u Node){
var匹配=[];
var匹配;
while(match=pattern.exec(child.data))
火柴。推(火柴);
对于(变量i=matches.length;i-->0;)
callback.call(窗口、子对象、匹配项[i]);
}
}
}
<a href="www.domain.com">
/\bhttps?:\/\/[^\s<>"`{}|\^\[\]\\]+/;
// 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]);
        }
    }
}