Convert a string to regular expression ruby

前端 未结 5 1618
广开言路
广开言路 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:35

    Looks like here you need the initial string to be in single quotes (refer this page)

    >> str = '[\w\s]+'
     => "[\\w\\s]+" 
    >> Regexp.new str
     => /[\w\s]+/ 
    

提交回复
热议问题