Выразительный JavaScript: Регулярные выражения
http://habrahabr.ru/post/242695/
HTML
<pre>— car и cat
— pop и prop
— ferret, ferry, и ferrari
— Любое слово, заканчивающееся на ious
— Пробел, за которым идёт точка, запятая, двоеточие или точка с запятой.
— Слово длинее шести букв
— Слово без букв e</pre>
<textarea id="log"></textarea>
CSS
textarea {
width: 100%;
height: 500px;
}
JavaScript
verify(/ca[r|t]/,
["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(/ious\b/,
["how delicious", "spacious room"],
["ruinous", "consciousness"]);
verify(/\s[:\.,;]/,
["bad punctuation ."],
["escape the dot"]);
verify(/\w{7,}/,
["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))
log("Не нашлось '" + s + "'");
});
no.forEach(function(s) {
if (regexp.test(s))
log("Неожиданное вхождение '" + s + "'");
});
}
function log(txt) {
var area = document.getElementById('log');
console.log(txt);
area.value += txt + '\n';
}