Skip to content

Instantly share code, notes, and snippets.

@emad-elsaid
Created February 27, 2014 20:46
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emad-elsaid/9259204 to your computer and use it in GitHub Desktop.
Save emad-elsaid/9259204 to your computer and use it in GitHub Desktop.
regular expression testing playground with ruby and sinatra
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