2010-08-13 132 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