-2

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]}`)
 } 

5
  • 1
    I think you should look at your variable names, you are joining the array landmarkArr which i don't see. Then inside the match method you use a variable called landmarks which is also no where to be found. Commented Sep 29, 2020 at 10:07
  • 1
    Did you mean to write locationArr = ["brisbane", "paris", "london", "singapore", "dubai", "seattle"]; and then let locations = new RegExp("\\b(" + locationArr.join("|") + ")\\b", "gi");? Commented Sep 29, 2020 at 10:08
  • 1
    There is so much wrong with it, that I really recommend you to get an editor like Visual Studio Code, which tells you about some common mistakes, like using variables that dont exist Commented Sep 29, 2020 at 10:15
  • Thanks @WiktorStribiżew - it was just a typo when writing my question, the code itself has the correct variables, appreciate you checking rather than assuming :) I have edited to correct the typo and Mara below has answered with your comment. Thanks again Commented Sep 29, 2020 at 23:55
  • Then it is a duplicate of Match dynamic string using regex Commented Sep 30, 2020 at 7:23

1 Answer 1

0

You can take a look at this, I used a phrase to mock the text from readCommandText(true); As Wiktor suggested, you can compose your regex like this:

new RegExp("\\b(" + locationArr.join("|") + ")\\b", "gi");

const locationArr = ["brisbane", "paris", "london", "singapore", "dubai", "seattle"];
let locationsRegXp = new RegExp("\\b(" + locationArr.join("|") + ")\\b", "gi");
console.log(locationsRegXp)

function matchLoc() {
  let text = "I wanted to travel to seattle and london this year but 2020.."
  let result = text.match(locationsRegXp);
  console.log("location: " + result)
}

matchLoc();

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mara! I have no idea why this got downvoted, this was exactly what I was missing - how to add the boundary - really appreciate it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.