Skip to content

Commit 4d173af

Browse files
committed
Merge pull request #2 from johnotander/dont-allow-whitespace-for-exact-option
Update the exact regex to not allow trailing or leading whitespace
2 parents 6d1bc18 + 62a39f5 commit 4d173af

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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

55
A 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

3834
MIT

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
module.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
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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": {

test/test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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() {

0 commit comments

Comments
 (0)