gpt4 book ai didi

sinatra - 如何在 Sinatra 中创建不区分大小写的路由?

转载 作者:行者123 更新时间:2023-12-01 16:02:24 25 4
gpt4 key购买 nike

我正在玩 Sinatra,我想让我的一条路线不区分大小写。我尝试添加这样的路线:

get "(?i)/tileflood/?" do
end

但它与预期的/tileflood 的任何排列都不匹配。我在 rubular.com 上测试了以下正则表达式,它匹配得很好。我错过了什么吗?

\/(?i)tileflood\/?

最佳答案

您想要一个真正的正则表达式来表示您的路线:

require 'sinatra'
get %r{^/tileflood/?$}i do
request.url + "\n"
end

证明:

smagic:~ phrogz$ curl http://localhost:4567/tileflood
http://localhost:4567/tileflood

smagic:~ phrogz$ curl http://localhost:4567/tIlEflOOd
http://localhost:4567/tIlEflOOd

smagic:~ phrogz$ curl http://localhost:4567/TILEFLOOD/
http://localhost:4567/TILEFLOOD/

smagic:~ phrogz$ curl http://localhost:4567/TILEFLOOD/z
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body { text-align:center;font-family:helvetica,arial;font-size:22px;
color:#888;margin:20px}
#c {margin:0 auto;width:500px;text-align:left}
</style>
</head>
<body>
<h2>Sinatra doesn't know this ditty.</h2>
<img src='/__sinatra__/404.png'>
<div id="c">
Try this:
<pre>get '/TILEFLOOD/z' do
"Hello World"
end</pre>
</div>
</body>
</html>

smagic:~ phrogz$ curl http://localhost:4567/tileflaad
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body { text-align:center;font-family:helvetica,arial;font-size:22px;
color:#888;margin:20px}
#c {margin:0 auto;width:500px;text-align:left}
</style>
</head>
<body>
<h2>Sinatra doesn't know this ditty.</h2>
<img src='/__sinatra__/404.png'>
<div id="c">
Try this:
<pre>get '/tileflaad' do
"Hello World"
end</pre>
</div>
</body>
</html>

关于sinatra - 如何在 Sinatra 中创建不区分大小写的路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4521445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com