javascript - Need regex to match unformatted phone number syntax -


i need regex javascript match phone number stripped of characters except numbers , 'x' (for extension). here example formats:

12223334444 2223334444 2223334444x5555 

you guaranteed have minimum of 10 numerical digits, leading '1' , extension optional. there no limit on number of numerical digits may appear after 'x'. want numbers split following backreferences:

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

the parenthesis above demonstrate how want number split backreferences. first set of parenthesis assigned backreference $1, example.

so far, here i've come regex. keep in mind i'm not great regex, , regexlib.com hasn't helped me out in department.

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

the above regex handles 2nd case in list of example test cases in first code snippet above. however, regex needs modified handle both optional '1' , extension. on this? thanks!

regex option seems fine 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 entered incorrectly'); 

this 12223334444 when there no extension

i expect want tweak out some, let me know how should be.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -