[a-z]
. This kind of character class is common enough that there is a shortcut for it, although it includes a few extra characters as well.
The closest character class in JavaScript to match the alphabet is \w
. This shortcut is equal to [A-Za-z0-9_]
. This character class matches upper and lowercase letters plus numbers. Note, this character class also includes the underscore character (_
).
let longHand = /[A-Za-z0-9_]+/;These shortcut character classes are also known as
let shortHand = /\w+/;
let numbers = "42";
let varNames = "important_var";
longHand.test(numbers); // Returns true
shortHand.test(numbers); // Returns true
longHand.test(varNames); // Returns true
shortHand.test(varNames); // Returns true
shorthand character classes
.
\w
to count the number of alphanumeric characters in various quotes and strings.
\w
to match all characters which are alphanumeric.');
- text: Your regex should find 31 alphanumeric characters in "The five boxing wizards jump quickly."
testString: assert("The five boxing wizards jump quickly.".match(alphabetRegexV2).length === 31, 'Your regex should find 31 alphanumeric characters in "The five boxing wizards jump quickly."
');
- text: Your regex should find 32 alphanumeric characters in "Pack my box with five dozen liquor jugs."
testString: assert("Pack my box with five dozen liquor jugs.".match(alphabetRegexV2).length === 32, 'Your regex should find 32 alphanumeric characters in "Pack my box with five dozen liquor jugs."
');
- text: Your regex should find 30 alphanumeric characters in "How vexingly quick daft zebras jump!"
testString: assert("How vexingly quick daft zebras jump!".match(alphabetRegexV2).length === 30, 'Your regex should find 30 alphanumeric characters in "How vexingly quick daft zebras jump!"
');
- text: Your regex should find 36 alphanumeric characters in "123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."
testString: assert("123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.".match(alphabetRegexV2).length === 36, 'Your regex should find 36 alphanumeric characters in "123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."
');
```