Regular Expressions: Match Single Characters Not Specified

I was doing this exercise and as a result of a typo, thus thinking I didn’t understand the objective, decided to get a hint.
So I wonder if what is proposed in the solution is correct. I mean this:

let quoteSample = "3 blind mice.";
let myRegex = /[^aeiou^0-99]/ig; // Change this line
let result = quoteSample.match(myRegex); // Change this line

We use two carets ^ both before the vowels and before the numbers. I did a little research and now I am confused as to why we should put two carets. I tried typing only one like so:

/[^aeiou0-99]/ig;

and it does exactly the same thing. I wonder if there is any specific reason for that.
Also it is probably better to exclude numbers between 0-9 like so:

/[^aeiou0-9]/ig;

as there seems to be no difference.
I wonder if I get it right. Please, someone, correct me. Thanks in advance))

I don’t think the regex engine has a concept of numbers except as single characters - digits. So /[^aeiou0-99]/ig should achieve the same thing as /[^aeiou0-9]/ig. In both cases the engine interprets 0-9 as excluding any digits between 0-9 (so, all digits, which means all numbers). In the first regex it then gets to another 9 and goes "oh yeah, and also exclude 9… even though I already excluded it per 0-9 but whatever man). So the regex should work as intended, but it has redundancy.

Also I don’t think there is a need for second caret there. In fact, I think it would be interpreted as another character to exclude, along with the vowels. Someone correct me if I’m wrong.

2 Likes

@ArtemPetrov is exactly right – use the above regex on https://www.regextester.com/ and you’ll see that your regex is also excluding the caret character. And the redundant nine is… doubly redundant? lol

1 Like

Thanks everybody for helping. Now I feel more positive about the correctness of my code. Also thanks for a great tool for testing regex.

I also found another great website that lets you check regex. Feel free to check it out https://regexr.com/. Hope you enjoy it)

I actually tried this a slightly simpler way, but i wonder how efficient this would be compared to a range of numbers given instead.

let myRegex = /[^aeiouint]/ig;

Well, it will match all letters except:a e i o u i n t, AND all numbers ( because they are not excluded via ^ )