What is the Ruby equivalent of preg_quote()?

后端 未结 1 1468
梦毁少年i
梦毁少年i 2021-01-18 15:38

In PHP you need to use preg_quote() to escape all the characters in a string that have a particular meaning in a regular expression, to allow (for example)

相关标签:
1条回答
  • 2021-01-18 16:03

    You want Regexp.escape.

    str = "[...]"
    re = /#{Regexp.escape(str)}/
    "la[...]la[...]la".gsub(re,"") #=> "lalala"
    
    0 讨论(0)
提交回复
热议问题