1

I'm trying to figure out a way to parse a document for phone numbers (in several formats) so that it automatically wraps them in a span. Here's the code I threw together.

findPhones = function(){
  var pNumber = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;  
  var findPhone = $('*:contains('+pNumber+')');
    findPhone.each(function(i) {
      $(this).prepend('<span>');
        alert('Phone Number Found, delete me');
      $(this).append('</span>');          
    });
}

I have no idea how to check the entire document (or element for that matter) to see if it's a phone number.

Fiddle

j08691
  • 204,283
  • 31
  • 260
  • 272
Casey Dwayne
  • 2,142
  • 1
  • 17
  • 32

1 Answers1

0

You'll want to search all text nodes. An easy (and potentially awful) way to do this is by using document.body.innerHTML, but this can lead to severe disruption of other JavaScript that is bound to particular DOM elements. I recommend you use a NodeIterator or TreeWalker to do it, so that you can treat text nodes individually without disrupting the DOM. Here's an answer to a similar question that you might find useful (disclaimer: I wrote that answer).

Community
  • 1
  • 1
zetlen
  • 3,609
  • 25
  • 22