Skip to content

Instantly share code, notes, and snippets.

@Kyly
Created August 29, 2014 09:07
Show Gist options
  • Save Kyly/cb367ba967d0fdfec6d0 to your computer and use it in GitHub Desktop.
Save Kyly/cb367ba967d0fdfec6d0 to your computer and use it in GitHub Desktop.
Script to give a live display of how regexp works.
jQuery(function($) {
var formGroup = $('.form-group.has-feedback');
var formIcon = $('.form-group .glyphicon');
$('#regexp-text').on('input', function() {
var regexp = $(this).val();
selector(regexp);
});
function selector(regexp_str) {
var sampleText = $('#sample-text pre.sample');
var result = $('#sample-text pre.result');
if (regexp_str === '')
return;
try {
var regexp = new RegExp(regexp_str, 'gm');
reportSuccess();
sampleText.html(sampleText.html().replace(/(<([^>]+)>)/ig, ''));
if (regexp.test(sampleText.html())) {
result.text(sampleText.html().match(regexp));
sampleText.html(sampleText.html().replace(regexp,
"<strong>$&</strong>"));
}
} catch (err) {
sampleText.html(sampleText.html().replace(/(<([^>]+)>)/ig, ''));
reportWarning();
result.text('');
}
}
function reportSuccess() {
formGroup.addClass('has-success');
formIcon.addClass('glyphicon-ok');
formGroup.removeClass('has-warning');
formIcon.removeClass('glyphicon-warning-sign');
}
function reportWarning() {
formGroup.addClass('has-warning');
formIcon.addClass('glyphicon-warning-sign');
formGroup.removeClass('has-success');
formIcon.removeClass('glyphicon-ok');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment