2013-01-23 38 views
0

我需要一個正則表達式來從字符串中提取丹麥語電話號碼。我得到這個需要正則表達式爲丹麥語電話號碼

var phrase = "Text text text 11 22 33 44 text text."; 
phrase = phrase.replace(/^((\(?\+45\)?)?)(\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2})$/, "replace phone number"); 
alert(phrase); 

我發現這個鏈接,但它不工作: http://www.dbsoftlab.com/etl-tools/regular-expressions/is-danish-phone-number.html

+1

很好看的前桑德蘭門將鑽研Web開發的世界。 – rrrr

回答

1

在您嘗試使用表達式,如果電話號碼是字符串中的唯一的事情只會匹配,因爲^字符串和$匹配的開頭匹配結束。

我認爲,這是一個更簡單的正則表達式,將工作:

/([0-9]{2}){3}[0-9]{2}/ 

所以,你可以說textStr.replace(/([0-9] {2}){3} [0- 9] {2}/g,'new string')。

入住在這裏:http://refiddle.com/gk8

+0

這似乎按預期工作。謝謝 :) –

0

問題是^$,分別匹配字符串的開頭和結尾其中。由於數字位於字符串的中間,因此它們不匹配。刪除它們,它的工作原理:

> var phrase = "Text text text 11 22 33 44 text text."; 
undefined 
> phrase = phrase.replace(/((\(?\+45\)?)?)(\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2})/, " replace phone number"); 
'Text text text replace phone number text text.'