2010-08-13 133 views
2

我想要从html文档捕获电话号码。我使用了一个模式,但它也选择了标签属性部分的数字。从html文本中捕获数字

var myArray = $('body').html().match(/([\s()-.])*(0[1-9]|[+][1-9]|[+][+][1-9]|00[1-9])(([\s()-.\/])*[0-9]+)*/g); 
+2

这就是为什么它不理解HTML:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Borealid 2010-08-13 17:02:38

+0

+1为了让我轻笑。 – 2010-08-13 17:46:50

回答

1

jquery中的html()方法使用innerHTML JavaScript属性。 innerHTML属性在运行时检索元素的来源。 如果您需要元素的纯文本内容(不带HTML标签),请使用innerText和textContent属性。

实施例:

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); 

有关进一步的细节和例子: innerText propertytextContent propertyinnerHTML property