Skip to content

Instantly share code, notes, and snippets.

@GregPK
Last active December 20, 2015 06:49
Show Gist options
  • Save GregPK/6088655 to your computer and use it in GitHub Desktop.
Save GregPK/6088655 to your computer and use it in GitHub Desktop.
A helper for http://regexcrossword.com. It helps identify which patterns are not met in a non-boolean fashion. It's a spoiler, so use sparringly. Also, please note that not all situations are accounted for (empty spaces, greedy vs non-greedy matching).
window.CrossCrutch = {}
window.CrossCrutch.check = function() {
var messages = [];
$(".puzzle table tbody tr").each(function (i,e) {
var $row = $(e);
var $cols = $row.find("th,td");
var $rowHeader = $cols.filter("th");
var $other = $cols.filter("td");
var re = new RegExp($rowHeader.html());
var txt = "";
$other.each(function(i,e) {
txt += $(e).find("input").val().toUpperCase();
});
if (!txt.match(re)) {
messages.push("Pattern "+re+" does not match ["+txt+"]");
$rowHeader.css("color","#FF0000");
}
else {
$rowHeader.css("color","");
}
});
$(".puzzle table thead th:not(:first)").each(function (iRow,e) {
var $headCell = $(e);
var re = new RegExp($headCell.find("span").html());
var txt = "";
var $rows = $(".puzzle table tbody tr");
$rows.each(function(i,e) {
txt += $(e).find("input")[iRow].value.toUpperCase();
});
if (!txt.match(re)) {
messages.push("Pattern "+re+" does not match ["+txt+"]");
$headCell.css("color","#FF0000");
}
else {
$headCell.css("color","");
}
});
var $msgs = $("#msgs");
if (messages.length > 0) {
if ($msgs.size() < 1) {
$("#main form:first").before('<div class="alert alert-error" id="msgs" ></div>');
$msgs = $("#msgs");
}
else
$msgs.find('*').remove();
var msg, _i, _len;
for (_i = 0, _len = messages.length; _i < _len; _i++) {
msg = messages[_i];
$msgs.append($("<p></p>").html(msg));
}
}
else {
$("#msgs").hide();
}
};
$("#main form").on('click','input[type=submit]',window.CrossCrutch.check);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment