Problem with a regex challenge

so I’m trying to understand clearly something in this example given by the curriculum:

let longHand = /[A-Za-z0-9_]+/;
let shortHand = /\w+/;
let numbers = "42";
let varNames = "important_var";
numbers.match(longHand);//this will return the string '42'.
numbers.match(shortHand);//this will return the string '42'.
varNames.match(longHand);//this will return the string "important_var".
varNames.match(shortHand);//this will return the string "important_var".

if you look at the variable varNames which contains an underscore,
why the shorthand character set ( \w) can match even the underscore character in the example above when it says in the challenge that it matches all letters and numbers? (underscore is not in those categories in my humble English skills)
thank you.

The character class \w includes the underscore. It’s equivalent to [A-Za-z0-9_]. You didn’t provide a link to the challenge so I’m not sure what the instructions say, but if they left out the underscore then that it just a mistake.

I have seen the long version of \w in the challenge and it includes an underscore, but i thought the underscore there is used just as a sign to make this expression special for a purpose, like when they use a caret ^ so the reader can understand that the regex is to find the first pattern for exemple… I didn’t expect the underscore in that regex expression is also one of the characters to be matched.
You see what i mean?
That was my confusion, the title of the challenge is “match all letters and numbers”:
This is the challenge’s link.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.