1

i'm trying to find an expression that matches a phone number in the format of (0[2,3,6,7,8,or 9]) XXXXXXXX where X is a digit, the space must be matched and so must the parentheses

My current expression is:

/\b\(0[236789]\)\s(\d){8}\b/g

but it's not picking up any test numbers such as

(02) 12345678

I know regex phone number questions get spammed on SO. I have been reading through all the ones I can find which is how I've made it to this point but I can't for the life of me figure this out.

3
  • 3
    \b stands for word boundary probably the strings you're testing don't start and end with a word boundary. Commented Aug 18, 2016 at 2:48
  • Ah, I thought it was for ensuring your pattern was at the start of the value being tested. Commented Aug 18, 2016 at 3:00
  • That will be /^\(0[236789]\)\s(\d){8}$/ where ^ stands for string start and $ for string end. Commented Aug 18, 2016 at 3:01

2 Answers 2

1

Just remove the \b on either end, and it should work

/\(0[236789]\)\s(\d){8}/g

This will match multiple phone numbers, not sure if that is what you want. If you want to make sure the entire string from start to finish is the full phone number, you can do this

/^\(0[236789]\)\s(\d){8}$/

This will match (02) 12345678, and won't work if there are any characters around the string.

Sign up to request clarification or add additional context in comments.

1 Comment

Such a simple mistake I was making but I had no idea, Thank you for that, was just testing it and it all came back fine.
0

Well, this is the best I can come up with.

/^[+]?(1\-|1\s|1|\d{3}\-|\d{3}\s|\d{2}\-|\d{2}\s|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3}) 
(\-|\s)?(\d{4})$/g

This should support all combinations like

  1. (541) 754-3010
  2. +1-541-754-3010
  3. 1-541-754-3010
  4. 001-541-754-3010
  5. 191 541 754 3010
  6. +91 541 754 3010

and so on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.