File tree Expand file tree Collapse file tree 4 files changed +12
-8
lines changed
Expand file tree Collapse file tree 4 files changed +12
-8
lines changed Original file line number Diff line number Diff line change 11# Phone Regex
22
3- [ ![ Build Status] ( https://travis-ci.org/johnotander /phone-regex.svg?branch=master )] ( https://travis-ci.org/johnotander /phone-regex )
3+ [ ![ Build Status] ( https://travis-ci.org/regexps /phone-regex.svg?branch=master )] ( https://travis-ci.org/regexps /phone-regex )
44
55A regular expression for matching phone numbers.
66
@@ -29,10 +29,6 @@ phone().test('apple') // => false
2929
3030* Regex from < http://stackoverflow.com/a/16702965/1378668 > .
3131
32- ## TODO
33-
34- - [ ] Don't let the regex match strings with whitespace, ` " 1234567890 " ` , with the exact option.
35-
3632## License
3733
3834MIT
Original file line number Diff line number Diff line change 22
33module . exports = function ( options ) {
44 options = options || { } ;
5- var regexBase = '\\s* (?:\\+?(\\d{1,3}))?[-. (]*(\\d{3})[-. )]*(\\d{3})[-. ]*(\\d{4})(?: *x(\\d+))?\\s* ' ;
5+ var regexBase = '(?:\\+?(\\d{1,3}))?[-. (]*(\\d{3})[-. )]*(\\d{3})[-. ]*(\\d{4})(?: *x(\\d+))?' ;
66
77 return options . exact ? new RegExp ( '^' + regexBase + '$' ) :
8- new RegExp ( regexBase , 'g' ) ;
8+ new RegExp ( '\\s*' + regexBase + '\\s*' , 'g' ) ;
99}
Original file line number Diff line number Diff line change 11{
22 "name" : " phone-regex" ,
3- "version" : " 1 .0.0" ,
3+ "version" : " 2 .0.0" ,
44 "description" : " Regular expression for phone numbers." ,
55 "main" : " index.js" ,
66 "directories" : {
Original file line number Diff line number Diff line change @@ -32,6 +32,14 @@ describe('phone-regex', function() {
3232 it ( 'should not find a phone number if it is not exact' , function ( ) {
3333 assert . equal ( phone ( { exact : true } ) . test ( 'apples 1 123 456 7890' ) , false ) ;
3434 } ) ;
35+
36+ it ( 'should not find a phone number if there is leading whitespace' , function ( ) {
37+ assert . equal ( phone ( { exact : true } ) . test ( ' 1 123 456 7890' ) , false ) ;
38+ } ) ;
39+
40+ it ( 'should not find a phone number if there is trailing whitespace' , function ( ) {
41+ assert . equal ( phone ( { exact : true } ) . test ( '1 123 456 7890 ' ) , false ) ;
42+ } ) ;
3543 } ) ;
3644
3745 describe ( 'g' , function ( ) {
You can’t perform that action at this time.
0 commit comments