Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.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查找电话号码并提供链接_Javascript_Regex - Fatal编程技术网

使用javascript查找电话号码并提供链接

使用javascript查找电话号码并提供链接,javascript,regex,Javascript,Regex,我试图使用javascript在页面中提供电话号码的链接。 有帮助,但它也会更新输入框、href、src等中的内容。如何更新不包含标签属性和文本的电话号码“); var regex=/(\(\d{3}\)?)(\d{3}-)?\d{3}-\d{4}/g; var text=$(“body:first”).text(); text=text.replace(regex,“”); $(“body:first”).html(文本); 请试试这个 演示:查看以下内容: 您可以通过修改正则表达式来实现

我试图使用javascript在页面中提供电话号码的链接。 有帮助,但它也会更新输入框、href、src等中的内容。如何更新不包含标签属性和文本的电话号码“);
var regex=/(\(\d{3}\)?)(\d{3}-)?\d{3}-\d{4}/g;
var text=$(“body:first”).text();
text=text.replace(regex,“”);
$(“body:first”).html(文本);
请试试这个

演示:

查看以下内容:


您可以通过修改正则表达式来实现这一点,但如果您使用新的修改过的html替换整个
正文
,那么元素等上的所有事件侦听器都将停止工作。

因为您需要更新不属于标记属性的电话号码,而不是锚标记内的内容,您可以将正则表达式更改为使用负数向前看

这将是正则表达式-

((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}(?!([^<]*>)|(((?!<a).)*<\/a>))
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Modified part
对于锚定标签中的内容-

(((?!<a).)*<\/a>)
((?!。

您可以检查正则表达式的匹配项。

@kamilkp我有一些更改,请检查
// match every element in body, go to its contents
$("body:first").find('*').contents().filter(function(){
    //if it's a text node, if its value matches the regex an it's not a child of an anchor
    if(this.nodeType == 3 && regex.test(this.nodeValue) && this.parentElement.tagName !== "A"){
        // create an anchor
        var anchor = document.createElement('a');
        // set its attribute to the phone number
        anchor.setAttribute('href', this.nodeValue.match(regex)[0]);
        // set the anchor's contents to be the same as its href attribute value
        anchor.appendChild(document.createTextNode(this.nodeValue.match(regex)[0]));

        // append it after the original text node
        if(this.nextSibling)
            this.parentElement.insertBefore(anchor, this.nextSibling);
        else
            this.parentElement.appendChild(anchor);

        // remove the phone number from the original text node
        this.nodeValue = this.nodeValue.replace(regex, '');
    }
});
((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}(?!([^<]*>)|(((?!<a).)*<\/a>))
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Modified part
([^<]*>)
(((?!<a).)*<\/a>)