最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Phone Number Recognition in Javascript - Stack Overflow

programmeradmin1浏览0评论

Is there a javascript library the can recognize phone numbers in a web page? Just like what skype did on their firefox plugin.

Or do you know a way on how to do it? Websites or any tutorial that do the same would be very helpful.

Your reply is greatly appreciated.

Best,

Is there a javascript library the can recognize phone numbers in a web page? Just like what skype did on their firefox plugin.

Or do you know a way on how to do it? Websites or any tutorial that do the same would be very helpful.

Your reply is greatly appreciated.

Best,

Share Improve this question asked May 10, 2010 at 23:26 mr.bmr.b 2,7088 gold badges40 silver badges65 bronze badges 3
  • A regular expression could be used to find phone numbers. A very basic one would be something like /\d\d\d-\d\d\d-\d\d\d\d/. But what do you want to do with the numbers you find? – user113716 Commented May 10, 2010 at 23:33
  • I wanted to convert all the phone numbers in a page into a link. Then I will hook it up into my application. Probably a pop-up will show when the link is clicked. – mr.b Commented May 10, 2010 at 23:44
  • You can always just look at how Skype did it. Download the FF extension (the .xpi file) and then unzip it (.xpi's are just .zip's with a different name) and start looking around. I haven't done this myself so I have no idea if it will be easy to find, but it's worth a try. Let us know what you find out! – Tyler Commented Jun 18, 2010 at 20:35
Add a ment  | 

3 Answers 3

Reset to default 3
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);

}

Someone else may have a better way of doing this, but this seems to give you a link around each phone number.

I just used my simple regular expression, so you may want to substitute the one that Adam provided.

$(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 it helps.


EDIT:

It may work as well, or better, with this version.

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

I don't know if there are any pitfalls, but seems to work with a fairly simple page.

To find matches within a string, you'll want to use a regular expression. This one (although somewhat lengthy) works pretty well:

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

(found here)

This will match "2405525009", "1(240) 652-5009", and "240/752-5009 ext.55", but not "(2405525009" or "2 (240) 652-5009".

To find all matches, you may want to repeatedly apply the exec() method in a while loop, as seen here.

发布评论

评论列表(0)

  1. 暂无评论