Tell us what’s happening:
Describe your issue in detail here.
In this course, the description shown as follows:
let longHand = /[A-Za-z0-9_]+/;
let shortHand = /\w+/;
let numbers = ....
However, when I tried /\w+/g in the practice, I failed to pass.
Instead, I have to remove the plus sign.
I am not sure what plus sign means here. Hope someone can help me.
Your code so far
let quoteSample = "The five boxing wizards jump quickly.";
let alphabetRegexV2 = /\w/ig; // Change this line
let result = quoteSample.match(alphabetRegexV2).length;
Your browser information:
User Agent is:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15
Challenge: Match All Letters and Numbers
Link to the challenge:
Hi @kuohungyi619 .
I hope not to make it more confusing that it already is. Regex is kind of tricky so…
+ symbol matchs a character (or group of characters) that appears one or more times in a row. This means that any character that you choose can be repeated 1 o more times as you wish. Example:
a+ matches aa, aaa, aaa, etc.
Now, as the challenge states
\w matches upper and lowercase letters plus numbers and underscore, that means this set of characters
\w corresponds to all those characters, when you use
+ you’re telling it that it should match any of those characters but it can be followed by one or more other character just as the
a example. As a result you could have something like “abcd” matched or a complete word like “five”.
a+ matches aa, aaa, aaa => but you’re only looking for the character a.
\w+ matches aa, ab, abc => since w matches all the alphabet characters, it can be any.
And what happens in the challenge when you use
\w+ is that your regex will first find a match of a character but this character can be followed by any other character, and what is this? a word. First set of characters is “the” but you’re also using the g flag (which searches a pattern more than once) so it is not going to finish there with the first word, it’ll search again for that pattern again (
\w+) so it’ll find “five” now. Count how many times that pattern matches your string or in other words how many words your string has.
However, if you omit the character (
\w), you’re only searching for a pattern that includes any of this characters [A-Za-z0-9_] but only once, so after finding the first character (letter in this case) your match will end and then will continue to search for the pattern again, matching the second letter and so on.
Try console.log() over the result variable to see what you get when you match alphanumerical characters (
\w ) and when you match words (
Thank you so much! Now I get it.
Have a nice day!
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.