Last active
December 28, 2016 05:17
-
-
Save ca0v/772c93159f506fca5fc6f67848069668 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// polyfill for RegExp.escape | |
if(!RegExp.escape){ | |
RegExp.escape = function(s){ | |
return String(s).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&'); | |
}; | |
} | |
function wildcard(pattern: string) { | |
let regex = new RegExp(pattern.split("*").map(RegExp.escape).join(".*")); | |
return (value: string) => { | |
let result = regex.test(value); | |
console.log(pattern, value, result); | |
return result; | |
}; | |
} | |
function go(testval: string) { | |
wildcard("")(testval); | |
wildcard("123456")(testval); | |
wildcard("*")(testval); | |
wildcard("*23456")(testval); | |
wildcard("12345*")(testval); | |
wildcard("*34*")(testval); | |
wildcard("*123456*")(testval); | |
wildcard("123*456")(testval); | |
wildcard("1.2*")(testval); | |
} | |
go("123456"); | |
go("12345"); | |
go("23456"); | |
go("1.2345"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment