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

Ruby不识别regex的g标志

  •  4
  • Zombies  · 技术社区  · 15 年前

    在str.scan中,默认情况下是否暗示?str[regex]中是否默认为关闭?

    2 回复  |  直到 15 年前
        1
  •  7
  •   sepp2k    15 年前

    是的,应用regex的频率取决于使用的方法,而不是regex的标志。

    扫描将返回一个数组,该数组包含(或迭代)regex的所有匹配项。比赛和 String#[] 将返回第一个匹配项。 =~ 将返回第一个匹配的索引。GSUB将替换所有出现的regex,SUB将替换第一个出现的regex。

        2
  •  3
  •   maček    15 年前
    smotchkkiss:~$ irb
    >> 'Foobar does not like food because he is a fool'.gsub(/foo/i, 'zim')
    => "zimbar does not like zimd because he is a ziml"