Javascript Phone Number Recognition

Is there a javascript library that can recognize phone numbers on a web page? In the same way that Skype did firefox on its plugin.

Or do you know how to do this? Sites or any tutorial that do the same will be very helpful.

Your answer is very welcome.

Best

+3
source share
3 answers

Someone may have a better way to do this, but that seems to give you a link to each phone number.

I just used my simple regular expression, so you can substitute the one provided by Adam .

$(document).ready(function () {

    $('*','body').each(function() {
        $(this).html( $(this).html().replace(/(\d\d\d-\d\d\d-\d\d\d\d)/g,'<a href="#">$1</a>') );
    });

});

Hope this helps.


EDIT:

, , .

$(document).ready(function () {
    $('body').html( $('body').html().replace(/(\d\d\d-\d\d\d-\d\d\d\d)/g,'<a href="#">$1</a>') );
});

, - , , , .

+2
var makePhoneLinks = function()
{
    var tNodes = [];
    getTextNodes(document.body,false,tNodes,/(((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4})/ig);                              
    var l = tNodes.length;
    while(l--)
    {
        wrapNode(tNodes[l],/(((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4})/ig,"<a target='#'>$1</a>");
    }  
}
function getTextNodes(node, includeWhitespaceNodes,textNodes,match) {

    if (node.nodeType == 3) {
        if (includeWhitespaceNodes || !/^\s*$/.test(node.nodeValue)) {
            if(match){
                if(match.test(node.nodeValue))
                    textNodes.push(node);
            }
            else {
                textNodes.push(node);
            }
        }
    } else {
        for (var i = 0, len = node.childNodes.length; i < len; ++i) {
            getTextNodes(node.childNodes[i],includeWhitespaceNodes,textNodes,match);
        }
    }

}
function wrapNode(n,match,m) {

    var temp = document.createElement('div');
    if(n.data)
        temp.innerHTML = n.data.replace(match, m);
    else{
        //whatever
    }
    while (temp.firstChild) {
        n.parentNode.insertBefore(temp.firstChild,n);

    }
    n.parentNode.removeChild(n);

}
+3

, . ( ) :

^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$

( )

"2405525009", "1 (240) 652-5009" "240/752-5009 ext.55", "(2405525009" "2 (240) 652-5009".

, exec() while, .

+2

Source: https://habr.com/ru/post/1744852/


All Articles