简体   繁体   中英

how to escape normal characters in regexp?

I'm running the latest version of sinatra and ruby 1.9.3

I let users create a custom regex in my sinatra app.

code:

Posts.all(:message => %r{(?i)(.*)#{params[:searchterm]}(.*)})

How do I escape #{params[:searchterm]} so that the user can't insert more regex?

(if the user searches for (.)*user he get's results that shouldn't work!

使用Regexp.escape转义变量值:

%r{(?i)(.*)#{Regexp.escape(params[:searchterm])}(.*)}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM