Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么是preg_quote()的Ruby等价物?_Ruby_Regex - Fatal编程技术网

什么是preg_quote()的Ruby等价物?

什么是preg_quote()的Ruby等价物?,ruby,regex,Ruby,Regex,在PHP中,需要使用preg_quote()对正则表达式中具有特定含义的字符串中的所有字符进行转义,以允许(例如)preg_match()搜索这些特殊字符 以下代码在Ruby中的等价物是什么 // The content of this variable is obtained from user input, in example. $search = "$var = 100"; if (preg_match('/' . preg_quote($search, '/') . ";/i")) {

在PHP中,需要使用
preg_quote()
对正则表达式中具有特定含义的字符串中的所有字符进行转义,以允许(例如)
preg_match()
搜索这些特殊字符

以下代码在Ruby中的等价物是什么

// The content of this variable is obtained from user input, in example.
$search = "$var = 100";
if (preg_match('/' . preg_quote($search, '/') . ";/i")) {
  // …
}

您需要
Regexp.escape

str = "[...]"
re = /#{Regexp.escape(str)}/
"la[...]la[...]la".gsub(re,"") #=> "lalala"