Randell Dawson 05f73ca409 fix(curriculum): Convert blockquote elements to triple backtick syntax for JavaScript Algorithms and Data Structures (#35992)
* fix: convert js algorithms and data structures

* fix: revert some blocks back to blockquote

* fix: reverted comparison code block to blockquotes

* fix: change js to json

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: convert various section to triple backticks

* fix: Make the formatting consistent for comparisons
2019-05-17 08:20:30 -05:00

3.1 KiB

id, title, challengeType
id title challengeType
587d7db8367417b2b2512ba0 Match Everything But Letters and Numbers 1

Description

You've learned that you can use a shortcut to match alphanumerics [A-Za-z0-9_] using \w. A natural pattern you might want to search for is the opposite of alphanumerics. You can search for the opposite of the \w with \W. Note, the opposite pattern uses a capital letter. This shortcut is the same as [^A-Za-z0-9_].
let shortHand = /\W/;
let numbers = "42%";
let sentence = "Coding!";
numbers.match(shortHand); // Returns ["%"]
sentence.match(shortHand); // Returns ["!"]

Instructions

Use the shorthand character class \W to count the number of non-alphanumeric characters in various quotes and strings.

Tests

tests:
  - text: Your regex should use the global flag.
    testString: assert(nonAlphabetRegex.global, 'Your regex should use the global flag.');
  - text: Your regex should find 6 non-alphanumeric characters in <code>"The five boxing wizards jump quickly."</code>.
    testString: assert("The five boxing wizards jump quickly.".match(nonAlphabetRegex).length == 6, 'Your regex should find 6 non-alphanumeric characters in <code>"The five boxing wizards jump quickly."</code>.');
  - text: Your regex should use the shorthand character.
    testString: assert(/\\W/.test(nonAlphabetRegex.source), 'Your regex should use the shorthand character to match characters which are non-alphanumeric.');
  - text: Your regex should find 8 non-alphanumeric characters in <code>"Pack my box with five dozen liquor jugs."</code>
    testString: assert("Pack my box with five dozen liquor jugs.".match(nonAlphabetRegex).length == 8, 'Your regex should find 8 non-alphanumeric characters in <code>"Pack my box with five dozen liquor jugs."</code>');
  - text: Your regex should find 6 non-alphanumeric characters in <code>"How vexingly quick daft zebras jump!"</code>
    testString: assert("How vexingly quick daft zebras jump!".match(nonAlphabetRegex).length == 6, 'Your regex should find 6 non-alphanumeric characters in <code>"How vexingly quick daft zebras jump!"</code>');
  - text: Your regex should find 12 non-alphanumeric characters in <code>"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."</code>
    testString: assert("123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.".match(nonAlphabetRegex).length == 12, 'Your regex should find 12 non-alphanumeric characters in <code>"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."</code>');

Challenge Seed

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

Solution

let quoteSample = "The five boxing wizards_jump quickly.";
let nonAlphabetRegex = /\W/g; // Change this line
let result = quoteSample.match(nonAlphabetRegex).length;