Created
April 12, 2019 19:00
-
-
Save brydavis/0e3a2d73be0110d1b38cd9215d442e9b to your computer and use it in GitHub Desktop.
Simple keyword 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
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