Un Programa para Experimentar con las Expresiones Regulares Usando Ajax

regular expression testing playground with ruby and sinatra

[~/sinatra/regexp(master)]$ cat regex-tester.rb 
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



Casiano Rodriguez León 2015-01-07