Skip to content

Instantly share code, notes, and snippets.

@brydavis
Created April 12, 2019 19:00
Show Gist options
  • Save brydavis/0e3a2d73be0110d1b38cd9215d442e9b to your computer and use it in GitHub Desktop.
Save brydavis/0e3a2d73be0110d1b38cd9215d442e9b to your computer and use it in GitHub Desktop.
Simple keyword search
var keywords = [
'music',
// 'news',
// 'weather',
// 'cooking',
// 'pasta',
'tech',
]
var source = [
"Create a database and tables",
"Set up AWS Workspaces",
"What is up with singing",
"Why is it cold outside",
"What time is it",
"How can I set up an appointment",
"How to make you're own music",
"Combining music and technology",
]
function searchKeywords(keywords, source) {
var re = new RegExp('(' + keywords.join('|') + ')', 'g')
var results = []
for (var i = 0; i < source.length; i++) {
const text = source[i];
var result = text.toLowerCase().match(re)
if (result != null || result != null) {
results.push(text)
}
}
return results
}
var results = searchKeywords(keywords, source)
console.log(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment