Last active
December 18, 2015 19:29
-
-
Save a5his/5833320 to your computer and use it in GitHub Desktop.
array wildcard search
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
| Array.prototype.search_with_wildcard = function(string) { | |
| // example string wildcards | |
| //string = "a*h*s"; | |
| regexp = new RegExp(/(\w+)|(\W{1})/g); | |
| string_wildcard_array = string.match(regexp); | |
| wildcard_regex = ['^']; | |
| $.each(string_wildcard_array, function(idx, val) { | |
| if(val === "*"){ | |
| wildcard_regex.push('[A-Za-z]'); | |
| } else{ | |
| wildcard_regex.push(val + '{1}'); | |
| } | |
| }); | |
| wildcard_regex.push('$') | |
| var regex_string = wildcard_regex.join(""); | |
| console.log(regex_string); | |
| var reg = new RegExp(regex_string); | |
| return this.filter(function(item){ | |
| return typeof item == 'string' && item.match(reg); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment