Basic Algorithm Scripting: Mutations with RegExp()

Hi Everyone, I’m really fascinated by the following code but I don’t understand why we are using the carrot symbol and the negation (!) symbol. Can anyone help me with this?

function mutation(arr) {
var re = new RegExp('[^'+arr[0]+']', "i");
return !re.test(arr[1]);

console.log(mutation(["hello", "he"]));

Challenge: Mutations

Link to the challenge:

when you have a character class in a regular expression and you put a caret as first character, it is matching anything but the characters inside the character class
so for example if you have [^aeiou] it will match anything but a vowel

the ! flip the boolean value

I admit I don’t understand why doing it this way
also because you need to test the first string, not the second

but the reason the test method is like that is because if you use [^aeiou] in a regular expression it will match if there is none of the vowels, but if you use [aeiou] it will match if there is any of the vowels
so the test method gives true if none of the letters are included in the other string, and false if any is included. flipping this with ! you get false if any of the letters are included, and false if they are not included.

ah, ok, with this explanation I also get why it is done in this way.

the regular expression is /[^hello]/i , when “hey” is tested it returns true because the y is not a character included in the other string. so the function returns false

so flipping with ! changes the logic by returning true if any of the characters in [^aeiou] are included and false if none of them are included? Why don’t we get the same answer/logic if ^ and ! are both taken out? It seems like the ! and the ^ are making things more difficult lol. This is blowing my mind

if you use [aeiou] you get true if any of them is included in the tested string.

the algorithm requires a true if all the characters of the second string are included in the first string

you could do without ^ and !
so now you have /[hello]/i.test("hey") - and it will return true, because at least one of the characters in the [hello] character class are in the tested string
! /[^hello]/i.test("hey")
in “hey” there is one character that is not in the character class, so it is matched by [^hello], so the test method returns true, and the function returns false - because not all the characters of the second string are in the first string

I understand now. that’s really tricky. Thanks!