I need a regular expression to convert US tel number to link

依然范特西╮ 提交于 2019-12-19 10:25:22

问题


Basically, the input field is just a string. People input their phone number in various formats. I need a regular expression to find and convert those numbers into links.

Input examples:

(201) 555-1212
(201)555-1212
201-555-1212
555-1212

Here's what I want:

<a href="tel:(201)555-1212">(201) 555-1212</a> - Notice the space is gone
<a href="tel:(201)555-1212">(201)555-1212</a>
<a href="tel:201-555-1212">201-555-1212</a>
<a href="tel:555-1212">555-1212</a>

I know it should be more robust than just removing spaces, but it is for an internal web site that my employees will be accessing from their iPhone. So, I'm willing to "just get it working."

Here's what I have so far in C# (which should show you how little I know about regular expressions):

strchk = Regex.Replace(strchk, @"\b([\d{3}\-\d{4}|\d{3}\-\d{3}\-\d{4}|\(\d{3}\)\d{3}\-\d{4}])\b", "<a href='tel:$&'>$&</a>", RegexOptions.IgnoreCase);

Can anyone help me by fixing this or suggesting a better way to do this?

EDIT:

Thanks everyone. Here's what I've got so far:

strchk = Regex.Replace(strchk, @"\b(\d{3}[-\.\s]\d{3}[-\.\s]\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]\d{4}|\d{3}[-\.\s]\d{4})\b", "<a href='tel:$1'>$1</a>", RegexOptions.IgnoreCase);

It is picking up just about everything EXCEPT those with (nnn) area codes, with or without spaces between it and the 7 digit number. It does pick up the 7 digit number and link it that way. However, if the area code is specified it doesn't get matched. Any idea what I'm doing wrong?

Second Edit:

Got it working now. All I did was remove the \b from the start of the string.


回答1:


Remove the [] and add \s* (zero or more whitespace characters) around each \-.

Also, you don't need to escape the -. (You can take out the \ from \-)

Explanation: [abcA-Z] is a character group, which matches a, b, c, or any character between A and Z.
It's not what you're trying to do.


Edits

In response to your updated regex:

  • Change [-\.\s] to [-\.\s]+ to match one or more of any of those characters (eg, a - with spaces around it)
  • The problem is that \b doesn't match the boundary between a space and a (.



回答2:


Afaik, no phone enters the other characters, so why not replace [^0-9] with '' ?




回答3:


Here's a regex I wrote for finding phone numbers:

(\+?\d[-\.\s]?)?(\(\d{3}\)\s?|\d{3}[-\.\s]?)\d{3}[-\.\s]?\d{4}

It's pretty flexible... allows a variety of formats.

Then, instead of killing yourself trying to replace it w/out spaces using a bunch of back references, instead pass the match to a function and just strip the spaces as you wanted.

C#/.net should have a method that allows a function as the replace argument...

Edit: They call it a `MatchEvaluator. That example uses a delegate, but I'm pretty sure you could use the slightly less verbose

(m) => m.Value.Replace(' ', '')

or something. working from memory here.



来源:https://stackoverflow.com/questions/3302482/i-need-a-regular-expression-to-convert-us-tel-number-to-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!