Created
February 27, 2014 20:46
-
-
Save emad-elsaid/9259204 to your computer and use it in GitHub Desktop.
regular expression testing playground with ruby and sinatra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' # gem install sinatra --no-ri --no-rdoc | |
set :port, 3000 | |
html = <<-EOT | |
<html><head><style> | |
#regex,#text{ width:100%; font-size:15px; display:block; margin-bottom:5px; } | |
#text{ height: 200px; } | |
span{ background:rgb(230,191,161); display:inline-block; border-radius:3px;} | |
</style></head><body> | |
<input id="regex" placeholder="Regex"/> | |
<textarea id="text" placeholder="Text"></textarea> | |
<div id="result"></div> | |
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> | |
<script> | |
$('#regex,#text').keyup(function(){ | |
$.get('/preview',{ | |
reg:$('#regex').val(), | |
text:$('#text').val() | |
},function(r){ | |
$('#result').html(r); | |
}); | |
}); | |
</script> | |
</body></html> | |
EOT | |
get('/'){ html } | |
get '/preview' do | |
begin | |
params['text'].gsub(/(#{params['reg']})/,'<span>\1</span>') | |
rescue | |
'Your regex is invalid' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment