Reputation: 2074
I've been googling and going through all of stackoverflow and not one regex worked.
I'm simply trying to grab the phone number from strings like this:
Phone: 123-456-7890
or
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam lobortis ullamcorper neque ut euismod. Curabitur convallis 1-800-123-4567 luctus posuere. Suspendisse placerat porta urna, vel bibendum odio aliquet ut. Quisque in posuere tortor. Vivamus sodales risus non dapibus posuere. Sed odio est.
Every regex string that I found online for phone validation doesn't work. I assume because the validation only expects the a number in the string, not text and white spaces.
Upvotes: 0
Views: 1102
Reputation:
A general solution:
alert("Phone: 123-456-7890".match(/\d+-\d+-\d+/))
Upvotes: 0
Reputation: 114
For regexp there are important things to consider.
The easiest, is there a delimiter that will always occur before or after the string you are searching for, such as "Phone:". If so, you can make a regex statement that searches for "Phone: " and takes everything after that, up until the next delimiter.
If you cannot predict what will come directly before or after the string, then you have to write the regex to search for something that resembles a phone number.
If so, will the phone number always be formatted the same?
If it is, then it is easy enough, search for 1[-][0-9]{3}[-][0-9]{3}[-][0-9]{4}
The above says, find strings that start with a 1 followed by a dash(-) followed by 3 consecutive digits 0-9, followed by a dash, followed by 3 more digits, followed by dash, followed by 4 more digits.
The problem is that being this specific has limitations, what if the phone number doesn't start with a 1? The point is you have to find out what will always be the case.
A phone number always has 3 digits dash 3 digits dash 4 digits, for our example, but sometimes it might have a country extension on the front, such as 1.
So perhaps you want to look for the 333-333-4444 format, and tell it to take any before that until it finds whitespace, etc.
Upvotes: 0
Reputation: 1330
Here is a regex example that can find a phone number like the one in your example. It might not work on some of the examples like were mentioned in the comments, but it may be what you are looking for. This function is taken directly from this related SO post. Run the snippet below to see the behavior.
var regex = new RegExp(
"\\+?\\(?\\d*\\)? ?\\(?\\d+\\)?\\d*([\\s./-]\\d{2,})+",
"g"
);
var possiblePhoneNumbers = ["Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam lobortis ullamcorper neque ut euismod. Curabitur convallis 1-800-123-4567 luctus posuere. Suspendisse placerat porta urna, vel bibendum odio aliquet ut. Quisque in posuere tortor. Vivamus sodales risus non dapibus posuere. Sed odio est.", "Find another one right here 1-888-999-1010 or can it?"];
for (var i=0; i < possiblePhoneNumbers.length; i++) {
document.write('<br />');
document.write(possiblePhoneNumbers[i] + ' => ');
document.write('<br />');
while (match = regex.exec(possiblePhoneNumbers[i])) {
document.write("Phone # in string: " + match[0]);
}
document.write('<br />');
}
Upvotes: 2