2

I want to capture phone numbers from an HTML document. I have used a pattern but its also selecting numbers that are the part of attributes of tags.

var myArray = $('body').html().match(/([\s()-.])*(0[1-9]|[+][1-9]|[+][+][1-9]|00[1-9])(([\s()-.\/])*[0-9]+)*/g);
Alex
  • 1,457
  • 1
  • 13
  • 26
coure2011
  • 40,286
  • 83
  • 216
  • 349
  • 2
    This is why it doesn't understand the HTML: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Borealid Aug 13 '10 at 17:02

1 Answers1

1

The html() method in jQuery uses the innerHTML JavaScript property. The innerHTML property retrieves the source of an element at runtime. If you need the pure text content of an element (without HTML tags), use the innerText and textContent properties.

Example:

var bodyText = (document.body.textContent === undefined) ? document.body.innerText : document.body.textContent;
var myArray = bodyText().match (/([\s()-.])*(0[1-9]|[+][1-9]|[+][+][1-9]|00[1-9])(([\s()-.\/])*[0-9]+)*/g);

For further details and examples:innerText property, textContent property, innerHTML property.

Alex
  • 1,457
  • 1
  • 13
  • 26
gumape
  • 2,832
  • 2
  • 18
  • 9