Skip to content

Instantly share code, notes, and snippets.

@bdefore
Last active August 29, 2015 14:01
Show Gist options
  • Save bdefore/9d31914155201967302a to your computer and use it in GitHub Desktop.
Save bdefore/9d31914155201967302a to your computer and use it in GitHub Desktop.
// . must match a single character
// * may match zero or more characters
// a-z must match that character
// order matters
assert(".", "a", true);
assert("..", "aa", true);
assert("..", "a", false);
assert("abc", "abc", true);
assert("ab", "abc", false);
assert("ab.", "abc", true);
assert("ab.", "abcd", false);
assert("ab..", "abcd", true);
assert("abc.", "abc", false);
assert(".c.", "acd", true);
assert("*", "", true);
assert("**", "", true);
assert("*c", "aaaac", true);
assert("*c*", "aaaac", true);
assert("*c.", "aaaac", false);
assert("a*b", "abc", false);
assert("a*", "abc", true);
assert("abc*", "abc", true);
assert("*c", "abc", true);
assert("a*b*c", "abac", true);
assert("a*b****.c", "abac", true);
assert("a*b****.c", "cab", false);
assert(".*b*.", "abc", true);
assert("*b*", "abc", true);
assert("*.b.*", "abc", true);
assert(".b.", "abc", true);
assert(".*c*.", "abc", false);
assert("*c*", "abc", true);
assert("*.c.*", "abc", false);
assert(".c.", "abc", false);
assert("abc", "ab", false);
assert("ab.....", ".......", false);
assert(".......", "...ab..", true);
assert("***ab****", "...ab...", true);
assert("***ab****", "ab", true);
assert("***ab***.", "ab", false);
assert("*.*", "", false);
assert("*.*", "a", true);
assert("*.*.*", "a", false);
assert("*.*.*", "ab", true);
function isMatch(pattern, sample) {
console.log("==============================================")
console.log('Testing match of ' + pattern + " to " + sample)
// For the purpose of matching, disregard stars in pattern
patternWithoutStars = pattern.replace(/\*/g, "");
if(patternWithoutStars.length > sample.length) {
console.log("Fails, patterns may not have more non-star characters than their sample.")
return false;
}
var lastMatch = -1;
for(var i = 0; i < patternWithoutStars.length; i++) {
var patternChar = patternWithoutStars[i];
// Test against a string of '.'s or the actual string
var testAgainst;
if(patternChar == ".") {
var testAgainst = sample.replace(/\S/g, ".");
}
else {
var testAgainst = sample;
}
// Find index of the match, beginning at index of last match
console.log("Checking to see if " + patternChar + " is in remainder of string: " + testAgainst.slice(lastMatch + 1))
lastMatch = testAgainst.indexOf(patternChar, lastMatch + 1);
if(lastMatch == -1) {
console.log("Didn't find it.")
return false;
}
console.log(patternChar + " matches " + testAgainst[lastMatch])
}
// Cover edge case where an additional letter is in pattern, but have reached end of sample
if(lastMatch < sample.length - 1 && pattern[pattern.length - 1] != "*") {
console.log("Had trailing letters that were not matched and no star was on original pattern. Failing.")
return false;
}
console.log("Match successful")
return true;
}
function assert(pattern, sample, expected) {
var result = isMatch(pattern, sample)
if(result != expected) {
throw new Error(pattern + " and " + sample + " should be: " + expected + " but got " + result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment