Skip to content

Instantly share code, notes, and snippets.

@yitonghe00
Created September 11, 2021 23:28
Show Gist options
  • Save yitonghe00/1b670d159afdd60c6dfd89420f2d84c6 to your computer and use it in GitHub Desktop.
Save yitonghe00/1b670d159afdd60c6dfd89420f2d84c6 to your computer and use it in GitHub Desktop.
// Fill in the regular expressions
verify(/ca[rt]/,
["my car", "bad cats"],
["camper", "high art"]);
verify(/pr?op/,
["pop culture", "mad props"],
["plop", "prrrop"]);
verify(/ferr(et|y|ari)/,
["ferret", "ferry", "ferrari"],
["ferrum", "transfer A"]);
verify(/ious\b/,
["how delicious", "spacious room"],
["ruinous", "consciousness"]);
verify(/\s[,.;:]/,
["bad punctuation ."],
["escape the period"]);
verify(/\w{7}/,
["Siebentausenddreihundertzweiundzwanzig"],
["no", "three small words"]);
verify(/\b[^\We]+\b/i,
["red platypus", "wobbling nest"],
["earth bed", "learning ape", "BEET"]);
function verify(regexp, yes, no) {
// Ignore unfinished exercises
if (regexp.source == "...") return;
for (let str of yes) if (!regexp.test(str)) {
console.log(`Failure to match '${str}'`);
}
for (let str of no) if (regexp.test(str)) {
console.log(`Unexpected match for '${str}'`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment