Convert a string to regular expression ruby

前端 未结 5 1625
广开言路
广开言路 2020-12-12 20:19

I need to convert string like \"/[\\w\\s]+/\" to regular expression.

\"/[\\w\\s]+/\" => /[\\w\\s]+/

I tried using different Regexp

5条回答
  •  情书的邮戳
    2020-12-12 20:38

    Using % notation:

    %r{\w+}m => /\w+/m
    

    or

    regex_string = '\W+'
    %r[#{regex_string}]
    

    From help:

    %r[ ] Interpolated Regexp (flags can appear after the closing delimiter)

提交回复
热议问题