I'm trying to extract phone number from a string which can be empty or look like one of the following examples:
- "Tél. : 04 27 52 12 87 Port. : 07 67 11 06 87"
- "Tél. : 04 27 52 12 87"
- "Port. : 07 67 11 06 87"
In addition, a phone number could look like this: (0039) 234786 So the constant(s) are "Tél. :" and/or "Port. :"
The have put these numbers in two variables (cell & landline).
I've tried split and indexOf functions but I'm pretty sure that there's a better and more efficient way to do this.
Any ideas ?
I'm trying to extract phone number from a string which can be empty or look like one of the following examples:
- "Tél. : 04 27 52 12 87 Port. : 07 67 11 06 87"
- "Tél. : 04 27 52 12 87"
- "Port. : 07 67 11 06 87"
In addition, a phone number could look like this: (0039) 234786 So the constant(s) are "Tél. :" and/or "Port. :"
The have put these numbers in two variables (cell & landline).
I've tried split and indexOf functions but I'm pretty sure that there's a better and more efficient way to do this.
Any ideas ?
Share Improve this question asked Apr 14, 2016 at 0:16 YannickHelmutYannickHelmut 5551 gold badge7 silver badges20 bronze badges 3-
Regular expressions are probably what you want. I notice your question is marked with the tag
regex
. What have you tried so far? – eestrada Commented Apr 14, 2016 at 0:25 - I do have to be honest, I didn't work a lot with regular expressions. Therefore I'm not fortable using and creating them... – YannickHelmut Commented Apr 14, 2016 at 0:27
- perfect solution with example : stackoverflow./a/39587209/4050261 – Adarsh Madrecha Commented Feb 8, 2019 at 13:47
3 Answers
Reset to default 4If you already know your string is a phone number, just replace all non-numeric characters to leave your phone number:
telInteger = parseInt(inputString.replace(/[^0-9]/g,''));
That way, it doesn't matter what the format is, you always get the number.
If you don't know your string has a phone number, and you're just searching in the wild, you can use the RegEx's in the other answers to detect a phone number.
Hope this helps
Here's a regular expression that looks for both versions of your phone number.
The two patterns are separated by a pipe character (|).
On the left side of the pipe, it looks for digit digit space four times in a row, then a final digit digit.
On the right side of the pipe, it looks for parenthesis, then four digits in a row, then end parenthesis, then a space, then six digits in a row.
I tested it in regexr. by copying and pasting your text and then in the top field, entering this:
/(((\d{2})(\s)){4}(\d){2})|(\((\d){4}\)(\s)(\d){6})/g
Note: regexr. supplies outer slashes, so no need to include that from the above example, and defaults to a global "g" flag at the end.
Using RegEx:
((Tél.\s:\s)(?([\d\s]+))?\s?([\d]+)?)?((Port.\s:\s)([\d\s]+))?
This should capture everything that you are trying to get.
I saw you said you don't have much experience with RegEx...me neither, but I just went through this tutorial (http://regexone./) this morning and I feel pretty confident using them now. It's really not that much to learn (at least to be able to do a whole lot with)!
Just so you know, I did check this with a RegEx tool and it worked!