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.
Afaik, no phone enters the other characters, so why not replace [^0-9]
with ''
?
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.
In response to your updated regex:
[-\.\s]
to [-\.\s]+
to match one or more of any of those characters (eg, a -
with spaces around it)\b
doesn't match the boundary between a space and a (
.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.