编程语言
首页 > 编程语言> > javascript – 使用正则表达式查找不在锚点中的页面上的电话号码

javascript – 使用正则表达式查找不在锚点中的页面上的电话号码

作者:互联网

我有这个正则表达式搜索电话号码模式:

[(]?\d{3}[)]?[(\s)?.-]\d{3}[\s.-]\d{4}

这与以下格式匹配的电话号码:

123 456 7890
(123)456 7890
(123) 456 7890
(123)456-7890
(123) 456-7890
123.456.7890
123-456-7890

我想扫描整个页面(使用JavaScript)查找此匹配,但不包括已存在于锚内的此匹配.
找到匹配后,我想将电话号码转换为移动设备的点击呼叫链接:

(123) 456-7890 --> <a href="tel:1234567890">(123) 456-7890</a>

我很确定我需要进行否定查找.我试过这个,但这似乎不是一个正确的想法:

(?!.*(\<a href.*?\>))[(]?\d{3}[)]?[(\s)?.-]\d{3}[\s.-]\d{4}

解决方法:

Don’t use regular expressions to parse HTML.使用HTML / DOM解析器获取文本节点(浏览器可以为您过滤掉它,删除锚标签,所有文本都太短,不能包含电话号码),您可以直接检查文本.

例如,使用XPath(这有点难看,但支持直接处理文本节点,大多数其他DOM方法没有):

// This query finds all text nodes with at least 12 non-whitespace characters
// who are not direct children of an anchor tag
// Letting XPath apply basic filters dramatically reduces the number of elements
// you need to process (there are tons of short and/or pure whitespace text nodes
// in most DOMs)
var xpr = document.evaluate('descendant-or-self::text()[not(parent::A) and string-length(normalize-space(self::text())) >= 12]',
                            document.body, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i=0, len=xpr.snapshotLength; i < len; ++i) {
    var txt = xpr.snapshotItem(i);
    // Splits with grouping to preserve the text split on
    var numbers = txt.data.split(/([(]?\d{3}[)]?[(\s)?.-]\d{3}[\s.-]\d{4})/);
    // split will return at least three items on a hit, prefix, split match, and suffix
    if (numbers.length >= 3) {
        var parent = txt.parentNode; // Save parent before replacing child
        // Insert new elements before existing element; first element is just
        // text before first phone number
        parent.insertBefore(document.createTextNode(numbers[0]), txt);

        // Now explicitly create pairs of anchors and following text nodes
        for (var j = 1; j < numbers.length; j += 2) {
            // Operate in pairs; odd index is phone number, even is
            // text following that phone number
            var anc = document.createElement('a');
            anc.href = 'tel:' + numbers[j].replace(/\D+/g, '');
            anc.textContent = numbers[j];
            parent.insertBefore(anc, txt);
            parent.insertBefore(document.createTextNode(numbers[j+1]), txt);
        }
        // Remove original text node now that we've inserted all the
        // replacement elements and don't need it for positioning anymore
        parent.removeChild(txt);

        parent.normalize(); // Normalize whitespace after rebuilding
    }
}

为了记录,基本过滤器在大多数页面上都有很多帮助.例如,在这个页面上,现在,正如我所看到的(将根据用户,浏览器,浏览器扩展和脚本等因素而不同)没有过滤器,查询的快照’descendant-or-self :: text() ‘会有1794项.省略锚标记的主要文本,’descendant-or-self :: text()[not(parent :: A)]’将其降为1538,以及完整查询,验证非空白内容至少为12字符长达87项.将正则表达式应用于87个项目是笨拙的改变,性能方面,并且您已经无需使用不合适的工具解析HTML.

标签:javascript,regex,negative-lookahead
来源: https://codeday.me/bug/20191002/1842293.html