代码之家  ›  专栏  ›  技术社区  ›  avpaderno

preg_quote()的红宝石等价物是什么?

  •  5
  • avpaderno  · 技术社区  · 15 年前

    在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")) {
      // …
    }
    
    1 回复  |  直到 8 年前
        1
  •  6
  •   sepp2k    15 年前

    你想要 Regexp.escape .

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