Why does /w work and /w+ not work?

Why does 1 work and 2 doesn’t?

  1. let alphabetRegexV2 = /\w/g;
  2. let alphabetRegexV2 = /\w+/g;

let quoteSample = "The five boxing wizards jump quickly.";
let alphabetRegexV2 = /change/; // Change this line
let result = quoteSample.match(alphabetRegexV2).length;

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36

Challenge: Match All Letters and Numbers

Link to the challenge:

you can use console.log(result) and see what the two searches output. The difference in the elements count is quite high. You can remove the lenght property from the result variable and actually display the results array. /\w/g would display the letters, while /\w+/g would give us the words. The later regex is greedy, it matches any number of subsequent letters.

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