I have a lengthy array of location names. I am trying to build a command which, when a phrase is typed into a text box, I can parse the phrase text to determine if any of the locations in my list are mentioned. I have done this successfully with a regex containing a small group of strings which I created manually, but with this array conversion I only get "undefined" as an outcome. My working function (created manually) is shown below, followed by the one I am struggling with. I think this is due to the structure of the RegEx somehow (maybe it needs brackets?) but I can work out how to find the issue. Any help in troubleshooting would be greatly appreciated
working code with manually created regex strings. Note, "readCommandText" is a function I created to read the text to be checked
let colorSet = /\b(red|green|blue)\b/;
function matchRobot() {
let text = readCommandText(true);
let result = text.match(colorSet);
console.log(`color: ${result[1]}`)
}
Non working code with array conversion (actual list of locations is much longer). No matter what location I input I still see "undefined"
What am I overlooking?
locationArr = [brisbane, paris, london, singapore, dubai, seattle];
let locations = new RegExp(locationArr.join("|"), "gi");
function matchLoc() {
let text = readCommandText(true);
let result = text.match(locations);
console.log(`location: ${result[1]}`)
}
landmarkArr
which i don't see. Then inside the match method you use a variable calledlandmarks
which is also no where to be found. – Reyno Sep 29 '20 at 10:07locationArr = ["brisbane", "paris", "london", "singapore", "dubai", "seattle"];
and thenlet locations = new RegExp("\\b(" + locationArr.join("|") + ")\\b", "gi");
? – Wiktor Stribiżew Sep 29 '20 at 10:08