Skip to content

Instantly share code, notes, and snippets.

@papayasoft
Last active January 15, 2016 18:44
Show Gist options
  • Save papayasoft/df030b5d6dd2d874c696 to your computer and use it in GitHub Desktop.
Save papayasoft/df030b5d6dd2d874c696 to your computer and use it in GitHub Desktop.
regex for pattern 00501 - 99999
var myTest = {
// Attempted test for pattern accepting anything 00501 - 999999
pattern: /^(0050[1-9]|005[1-9][0-9]|00[6-9][0-9]{2}|0[1-9]{4}|[1-9][0-9]{4})$/,
// Some sample good string
good: ['00501', '00502', '00509', '00590', '00599', '00600', '00699', '09999', '12345', '54321'],
// Some sample bad strings
bad: ['', '0', '12', '300', '900', '5555', '00500', '00499', '00500'],
// A test that echoes on a bad string
echoOnFail: function(x) {
if (!this.pattern.test(x)) {
console.log('Unexpected fail: ', x);
}
},
// a test that echoes on a good string
echoOnPass: function(x) {
if (this.pattern.test(x)) {
console.log('Unexpected pass: ', x);
}
},
test: function() {
this.good.forEach(this.echoOnFail.bind(this));
this.bad.forEach(this.echoOnPass.bind(this));
}
};
myTest.test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment