簡體   English   中英

僅當字符串中存在其他號碼時才提取電話號碼

[英]Extract Phone number only when other numbers are present in a string

如何改進此正則表達式以僅提取包含“-”或字符串中數字之間不存在字符的數字。 例如:

我的電話號碼是 647-871-9067。 2013 納稅年度。9,846.22 美元。

應該提取[647-871-9067]

或者

我的電話號碼是 6478719067。2013 納稅年度。9,846.22 美元。

應僅提取[6478719067]

這是我寫的正則表達式,但它正在提取2013和電話號碼

\b\s\(?([0-9-*)?]*)

下面的代碼正則表達式將只獲取電話號碼,而不考慮電話號碼之間的“-”。

var string1 = '647-871-9067. tax year 2013. $9,846.22',
string2 = '6478719067. tax year 2013. $9,846.22',
regx = /(\d{3}-?\d{3}-?\d{4})/g;

string1.match(regx)   // will return "647-871-9067"
string2.match(regx)   // will return "6478719067"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM