Skip to content

Instantly share code, notes, and snippets.

@headquarters
Created July 28, 2018 19:12
Show Gist options
  • Select an option

  • Save headquarters/65fa3231b42d2f8f67aa2df772bf1062 to your computer and use it in GitHub Desktop.

Select an option

Save headquarters/65fa3231b42d2f8f67aa2df772bf1062 to your computer and use it in GitHub Desktop.
Match a sequence of characters anywhere in a string, so long as the order is consistent.
var CSS_COLOR_NAMES = ["AliceBlue","AntiqueWhite","Aqua","Aquamarine","Azure","Beige","Bisque","Black","BlanchedAlmond","Blue","BlueViolet","Brown","BurlyWood","CadetBlue","Chartreuse","Chocolate","Coral","CornflowerBlue","Cornsilk","Crimson","Cyan","DarkBlue","DarkCyan","DarkGoldenRod","DarkGray","DarkGrey","DarkGreen","DarkKhaki","DarkMagenta","DarkOliveGreen","Darkorange","DarkOrchid","DarkRed","DarkSalmon","DarkSeaGreen","DarkSlateBlue","DarkSlateGray","DarkSlateGrey","DarkTurquoise","DarkViolet","DeepPink","DeepSkyBlue","DimGray","DimGrey","DodgerBlue","FireBrick","FloralWhite","ForestGreen","Fuchsia","Gainsboro","GhostWhite","Gold","GoldenRod","Gray","Grey","Green","GreenYellow","HoneyDew","HotPink","IndianRed","Indigo","Ivory","Khaki","Lavender","LavenderBlush","LawnGreen","LemonChiffon","LightBlue","LightCoral","LightCyan","LightGoldenRodYellow","LightGray","LightGrey","LightGreen","LightPink","LightSalmon","LightSeaGreen","LightSkyBlue","LightSlateGray","LightSlateGrey","LightSteelBlue","LightYellow","Lime","LimeGreen","Linen","Magenta","Maroon","MediumAquaMarine","MediumBlue","MediumOrchid","MediumPurple","MediumSeaGreen","MediumSlateBlue","MediumSpringGreen","MediumTurquoise","MediumVioletRed","MidnightBlue","MintCream","MistyRose","Moccasin","NavajoWhite","Navy","OldLace","Olive","OliveDrab","Orange","OrangeRed","Orchid","PaleGoldenRod","PaleGreen","PaleTurquoise","PaleVioletRed","PapayaWhip","PeachPuff","Peru","Pink","Plum","PowderBlue","Purple","Red","RosyBrown","RoyalBlue","SaddleBrown","Salmon","SandyBrown","SeaGreen","SeaShell","Sienna","Silver","SkyBlue","SlateBlue","SlateGray","SlateGrey","Snow","SpringGreen","SteelBlue","Tan","Teal","Thistle","Tomato","Turquoise","Violet","Wheat","White","WhiteSmoke","Yellow","YellowGreen"];
function search(term, dataset) {
var result = [];
dataset.forEach(function(item) {
var termArray = term.split('');
// start regex with 0 or more character match
var regex = '.*';
for(var i = 0; i < termArray.length; i++) {
// add character term to regex string
regex += termArray[i];
// allow matching 0 or more characters after this term
regex += '.*';
}
var regex = new RegExp(regex);
if (item.match(regex)) {
result.push(item);
};
});
return result;
}
console.log(search('iui', CSS_COLOR_NAMES)); // 'AntiqueWhite'
console.log(search('uqi', CSS_COLOR_NAMES)); // 'MediumAquaMarine'
console.log(search('lice', CSS_COLOR_NAMES)); // 'AliceBlue'
console.log(search('xyz', CSS_COLOR_NAMES)); // []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment