0

I have this regex format for a phone number sample 012-3456789.

malaysianPhoneRegex = "^[0]{1}[0-9]{2}[\\-]{1}[0-9]{7,8}$";

Now I want it to be able to accept 012-3456789 or 0123456789 or 012 345 6789 or 012 3456789

How do I do it, I am still learning on regex.

Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
Kingsley Simon
  • 2,090
  • 5
  • 38
  • 84
  • Try adding some examples of regex patterns you have tried but are not working for you. – Dave Bennett Apr 07 '15 at 03:59
  • There are a dozen or more questions on SO about parsing phone numbers with regexp. Search for them. –  Apr 07 '15 at 04:26

3 Answers3

0

Put the space, hyphen inside the character class and then make it as optional.

malaysianPhoneRegex = "^0[0-9]{2}[- ]?[0-9]{3} ?[0-9]{4,5}$";
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0
var regex = /^[0]{1}[0-9]{2}[ \-]{0,1}[0-9]{3} {0,1}[0-9]{4}$/;
test = function(string) {
  console.log(string + ': ' + regex.test(string));
}
test("012-3456789");
test("0123456789");
test("012 345 6789");
test("012 3456789");
bvaughn
  • 13,300
  • 45
  • 46
0

Try /\d*([- ]*)\d/g

Working demo here - http://regexr.com/3aouv

mark it as answer if it helps

Nilesh Thakkar
  • 1,442
  • 4
  • 25
  • 36