正则关卡18:匹配所有的字母和数字

正则表达式测试工具

使用​元字符​,可以使用[a-z]搜寻字母表中的所有字母。
这种元字符是很常见的,它有一个缩写,但这个缩写也包含额外的字符。
JavaScript 中与字母表匹配的最接近的元字符是\w,这个缩写等同于[A-Za-z0-9_]
它不仅可以匹配大小写字母和数字,​注意​,它还会匹配下划线字符(_)。

var longHand = /[A-Za-z0-9_]+/;
var shortHand = /\w+/;
var numbers = "58";
var varNames = "important_var";

console.log( longHand.test(numbers) ); // 返回 true
console.log( shortHand.test(numbers) ); // 返回 true
console.log( longHand.test(varNames) ); // 返回 true
console.log( shortHand.test(varNames) ); // 返回  true

闯关:使用缩写\w来计算所有引号中字母和数字字符的数量。

var quoteSample = "The five boxing wizards jump quickly.";
var alphabetRegexV2 = /change/; // 修改这行
var result = quoteSample.match(alphabetRegexV2);
console.log(result);

正确代码

var quoteSample = "The five boxing wizards jump quickly.";
var alphabetRegexV2 = /\w/g;
var result = quoteSample.match(alphabetRegexV2);
console.log(result);