Skip to content

Instantly share code, notes, and snippets.

@ca0v
Last active December 28, 2016 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ca0v/772c93159f506fca5fc6f67848069668 to your computer and use it in GitHub Desktop.
Save ca0v/772c93159f506fca5fc6f67848069668 to your computer and use it in GitHub Desktop.
// 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