function matchRuleExpl(str, rule) {
// for this solution to work on any string, no matter what characters it has
var escapeRegex = (str) => str.replace(/([.*+?^=!:${}()|[]\/\])/g, "\$1");
// "." => Find a single character, except newline or line terminator
// ".*" => Matches any string that contains zero or more characters
rule = rule.split("*").map(escapeRegex).join(".*");
// "^" => Matches any string with the following at the beginning of it
// "$" => Matches any string with that in front at the end of it
rule = "^" + rule + "$"
//Create a regular expression object for matching string
var regex = new RegExp(rule);
//Returns true if it finds a match, otherwise it returns false
return regex.test(str);
Hi, great work!
I did a quick and dirty hack for Searching not exact Names. I hope you can implement it (maybe in better Code) in default functions:
// Colornames: Double Words before Single Words, and 2 Letters at the end !!! (White = 3, Warm = 8, Rose = 4) var colornames = ['Black', 'Warm', 'White', 'Deep Red', 'Red', 'Orange', 'Yellow', 'Fern Green', 'Sea Green', 'Green', 'Cyan', 'Lavender', 'Blue', 'Violet', 'Magenta', 'Pink', 'CTO', 'CTB', 'Grey', 'Gr', 'Sw', 'Ws', 'Wm', 'Rt', 'Or', 'Ge', 'Gn', 'Tk', 'Cy', 'Bl', 'Vi', 'Ma', 'Pk']; var colorcodes = [ 0, 8, 3, 121, 5, 9, 13, 17, 25, 21, 33, 41, 45, 49, 53, 57, 108, 93, 1, 1, 0, 3, 8, 5, 9, 13, 21, 33, 33, 45, 49, 53, 57];
// ---------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////////////////////////// // HF edit
// Original / function findColorIndex(colorName) { //const names = ['Black', 'White', 'Red', 'Orange', 'Yellow', 'Fern Green', 'Green', 'Sea Green', 'Cyan', 'Lavender', 'Blue', 'Violet', 'Magenta', 'Pink', 'CTO', 'CTB', 'Grey', 'Deep Red']; //const index = names.indexOf(colorName); const index = colornames.indexOf(colorName); return index; } /
function findColorIndex(colorName) { //const names = ['Black', 'White', 'Red*', 'Orange', 'Yellow', 'Fern Green', 'Green', 'Sea Green', 'Cyan', 'Lavender', 'Blue', 'Violet', 'Magenta', 'Pink', 'CTO', 'CTB', 'Grey', 'Deep Red']; //const index = names.indexOf(colorName);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// HF edit // found at // https://stackoverflow.com/questions/26246601/wildcard-string-comparison-in-javascript
function matchRuleExpl(str, rule) { // for this solution to work on any string, no matter what characters it has var escapeRegex = (str) => str.replace(/([.*+?^=!:${}()|[]\/\])/g, "\$1");
} / Examples alert( "1. " + matchRuleShort("bird123", "bird") + "\n" + "2. " + matchRuleShort("123bird", "bird") + "\n" + "3. " + matchRuleShort("123bird123", "bird") + "\n" + "4. " + matchRuleShort("bird123bird", "birdbird") + "\n" + "5. " + matchRuleShort("123bird123bird123", "birdbird") + "\n" + "6. " + matchRuleShort("s[pe]c 3 re$ex 6 cha^rs", "s[pe]cre$excha^rs") + "\n" + "7. " + matchRuleShort("should not match", "should noooot match") + "\n" ); */