Skip to content

Instantly share code, notes, and snippets.

@Shiggiddie
Created October 28, 2014 11:43
Show Gist options
  • Save Shiggiddie/d4f158ab78eff21a53a8 to your computer and use it in GitHub Desktop.
Save Shiggiddie/d4f158ab78eff21a53a8 to your computer and use it in GitHub Desktop.
// Regex Golf
verify(/ca[rt]/,
["my car", "bad cats"],
["camper", "high art"]);
verify(/pr?op/,
["pop culture", "mad props"],
["plop"]);
verify(/ferr(et|y|ari)/,
["ferret", "ferry", "ferrari"],
["ferrum", "transfer A"]);
verify(/\w+(ious)\b/,
["how delicious", "spacious room"],
["ruinous", "consciousness"]);
verify(/\s[.,;:]/,
["bad punctuation ."],
["escape the dot"]);
verify(/\b\w{7,}\b/,
["hottentottententen"],
["no", "hotten totten tenten"]);
verify(/\b[a-df-z]+\b/,
["red platypus", "wobbling nest"],
["earth bed", "learning ape"]);
function verify(regexp, yes, no) {
// Ignore unfinished exercises
if (regexp.source == "...") return;
yes.forEach(function(s) {
if (!regexp.test(s))
console.log("Failure to match '" + s + "'");
});
no.forEach(function(s) {
if (regexp.test(s))
console.log("Unexpected match for '" + s + "'");
});
}
// Quoting Style
var text = "'I'm the cook,' he said, 'it's my job.'";
// Change this call.
console.log(text.replace(/(^|\W)'|'(\W|$)/g, '$1"$2'));
// → "I'm the cook," he said, "it's my job."
// Numbers Again
var number = /^(\+|-)?(\d+[.]?|[.]\d+|\d+[.]\d+)((E|e)(\+|-)?\d+)?$/;
// Tests:
["1", "-1", "+15", "1.55", ".5", "5.", "1.3e2", "1E-4",
"1e+12"].forEach(function(s) {
if (!number.test(s))
console.log("Failed to match '" + s + "'");
});
["1a", "+-1", "1.2.3", "1+1", "1e4.5", ".5.", "1f5",
"."].forEach(function(s) {
if (number.test(s))
console.log("Incorrectly accepted '" + s + "'");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment