Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一段闲置的代码:
details =~ /.#{action.name}.*/
如果action.name包含诸如“abcd”之类的常规字符串,则一切正常,但如果action.string包含特殊字符,例如.or /,则会出现异常。
action.name
action.string
.
/
有没有办法检查action.name字符串而不必\在 action.name 中的每个特殊字符之前放置?
\
您可以使用Regexp::escape转义所有特殊字符。
尝试:
details =~ /.#{Regexp.escape(action.name)}.*/