A way to solve this challenge with a regEx constructor?

Tell us what’s happening:

My first train of thought was to generate a regular expression from the second value of the given array. Then use that regEx to test the first value.

I’m having issues with the regEx constructor. I cant seem to get it to format the regEx in my intended way.

Any input is appreciated

Your code so far

function mutation(arr) {
  let arr1Split = arr[1].slice()
  console.log(arr1Split) // logs hey

  /* 
   now I want to use arr1SplitUp to construct a RegEx
   which I can then use to check if arr[0] has those same letters
  */

  let regEx = new RegExp([arr1Split], "i")
  console.log(regEx) // logs /hey/i 

  /*
   here is my issue. Why doesn't it log /[hey]/i ?

   what im trying to achieve is a regEx that will check for
   presence of these letters regardless of order or case.

   is it possible to use a constructor to build a regEx like that?

  */

  return regEx.test(arr[0])
}

mutation(["hello", "hey"]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Mutations

Link to the challenge:

The constructor takes a regex pattern, which is a string, you have given it an array with a string inside.

["hey"]

Is a JavaScript array with one element (the string “hey”)

"[hey]"

Is a string that, if it was passed to a regex constructor, would create a pattern that matches on a single “h”, “e” or “y” character.

So if my understanding is correct, even if my ReGex constructor worked the way I wanted it to, I would still fail the test:

mutation(["voodoo", "no"]) should return false

because my theoretical regex /[no]/i would return true on a test of voodoo

even though it wouldn’t pass all the tests, is there a way to use the RegExp constructor to build the regex /[hey]/i without imputing a regex literal?

Is it possible to extract “hey” from ["hello", "hey"] as a string, assign that string to a variable, and then input it into the RegExp constructor as new RegExp([variable], i);
to make the regEx: /[hey]/i

and if so how would I go about doing that?

You need to abandon this regex approach. It would be overly complex if it is even possible. Why? Because regex is looking for a specific pattern of characters. There is no way to know what pattern to write as the inputs (arguments to the function) change each time.

I understand the approach is flawed, and more importantly ~why~ its flawed. But now I’m simply curious if there is a way to build a regex like /[hey]/i using a RegExp constructor without inputting a regex literal.

Ah! Thank you! I’ve been trying to figure that out for much longer than I should have.

I knew I had to use the ${variable} syntax somehow but every time I tried I was getting errors. I was forgetting the backticks!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.