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
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
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.