string.sub
看起来它只替换了第一个实例。是否有可以替代所有模式的选项或其他方法?你能在 perl 之类的正则表达式中完成吗?
(我觉得像 r/blah/blah/)
... 并 +1 给任何能告诉我为什么 string.sub
只替换第一个匹配项的人?
最佳答案
String.gsub应该可以解决问题。
引用文档:
gsub(pattern, replacement) → new_str
Returns a copy of str with the all occurrences of pattern substituted for the second argument. The pattern is typically a Regexp; if given as a String, any regular expression metacharacters it contains will be interpreted literally, e.g.
\\d
will match a backlash followed byd
, instead of a digit.
关于ruby - 如何替换 ruby 中模式的每个实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1895295/