开发者

Need regex to match unformatted phone number syntax

I need a regex for Javascript that will match a phone number stripped of all characters except numbers and 'x' (for extension). Here are some example formats:

12223334444
2223334444
2223334444x5555

You are guaranteed to always have a minimum of 10 numerical digits, as the leading '1' and extension are optional. There is also no limit on the number of numerical digits that may appear after the 'x'. I want the numbers to be split into the following backreferences:

(1)(222)(333)(4444)x(5555)

The parenthesis above demonstrate how I want the number to be split up into backreferences. The first set of parenthesis would be 开发者_开发知识库assigned to backreference $1, for example.

So far, here is what I've come up with for a regex. Keep in mind that I'm not really that great with regex, and regexlib.com hasn't really helped me out in this department.

(\d{3})(\d{3})(\d{4})

The above regex handles the 2nd case in my list of example test cases in my first code snippet above. However, this regex needs to be modified to handle both the optional '1' and extension. Any help on this? Thanks!


Regex option seems perfectly fine to me.

var subject = '2223334444';
result = subject.replace(/^1?(\d{3})(\d{3})(\d{4})(x\d+)?$/mg, "1$1$2$3$4");
alert(result);
if(!result.match(/^\d{11}(?:x\d+)?/))
    alert('The phone number came out invalid. Perhaps it was entered incorrectly');

This will say 12223334444 when there is no extension

I expect you want to tweak this out some, let me know how it should be.


If I were you, I would not go with a regular expression for this — it would cause more headaches than it solved. I would:

  1. Split the phone number on the "x", store the last part in the extension.
  2. See how long the initial part is, 9 or 10 digits
    • If it's 10 digits, check that the first is a 1, slice it off, and then continue with the 9-digit process:
    • If it's 9 digits, split it up into 3-3-4 and split them into area code, exchange, number.
  3. Validate the area code and exchange code according to the rules of the NANP.

This will validate your phone number and be much, much easier and will make it possible for you to enforce rules like "no X11 area codes" or "no X11 exchange codes" more-easily — you'd have to do this anyway, and it's probably easier to just use plain string manipulation to split it into substrings.


I did a bit more testing and here's a solution I've found. I haven't found a case where this breaks yet, but if someone sees something wrong with it please let me know:

(1)?(\d{3})(\d{3})(\d{4})(?:x(\d+))?

Update: I've revised the regex above to handle some more edge cases. This new version will fail completely if something unexpected is present.

(^1|^)(\d{3})(\d{3})(\d{4})($|(?:x(\d+))$)


My regex is:

/\+?[0-9\-\ \(\)]{10,22}/g
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜