简体   繁体   English

仅当字符串中存在其他号码时才提取电话号码

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

How can I improve this regex to extract only numbers that contain "-" or no characters present between numbers inside string.如何改进此正则表达式以仅提取包含“-”或字符串中数字之间不存在字符的数字。 For Example:例如:

My phone number is 647-871-9067.我的电话号码是 647-871-9067。 tax year 2013. $9,846.22. 2013 纳税年度。9,846.22 美元。

Should Extract [647-871-9067]应该提取[647-871-9067]

or或者

My phone number is 6478719067. tax year 2013. $9,846.22.我的电话号码是 6478719067。2013 纳税年度。9,846.22 美元。

Should Extract [6478719067] only.应仅提取[6478719067]

This is the regex I have written but it is extracting 2013 along with phone number这是我写的正则表达式,但它正在提取2013和电话号码

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

Below code regular expression will fetch only the phone number irrespective of '-' in between the phone number.下面的代码正则表达式将只获取电话号码,而不考虑电话号码之间的“-”。

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