Tell us what’s happening:
I don’t understand why using the example that was given does not work like it does in the example.
example shows that /\w+/ should work but it does not. or at least that was not what passes the test.
whats i coded below is what passed the test.
Your code so far
let quoteSample = "The five boxing wizards jump quickly.";
let alphabetRegexV2 = /[\w*]/gi; // Change this line
let result = quoteSample.match(alphabetRegexV2).length;
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/match-all-letters-and-numbers
The working one is the /\w/gi .
If you print to the console what you found, you will see that the result is 6 for the test sentence. Meaning they will select all the words. If you remove the brackets, it will give the solution.
/\w+/
This reads one or more characters in [a-zA-Z0-9_]
. In other words, this means a single word. So, it matches with "The"
in the example.
/\w+/g
This will apply previous regex across the string. So, the result is an array containing all the words.
["The", "five", "boxing", "wizards", "jump", "quickly"]
As you see, this means count all the words in a string; this is obviously not what the exercise is asking for.
Now, what you did is sort of incorrect because of the *
in the character class []
.
When you put that in brackets, it doesn’t mean match zero or more anymore; it literally means match with *
character.
So, your regex translates to this
/[a-zA-Z0-9_*]/gi
Notice the i
modifier is redundant because \w
already includes uppercase and lowercase alphabets.
Also, notice that because you’ve added *
it will count more than the set of alphanumeric characters.
This is a fixed version of your regex
/[\w]/g
Now, I will explain why this works.
/[\w]/
will match any single alphanumeric character in a string.
/[\w]/g
applies above regex across the string, which means all alphanumeric characters in the string.
5 Likes