在 gsub 或 scan 中匹配位置

saw*_*awa 3 ruby position gsub ruby-1.9.3

=~使用gsubor时为每个匹配实现匹配位置(由 返回的索引)的最佳方法是什么scan

Nas*_*ges 5

"hello".gsub(/./) { Regexp.last_match.offset(0).first }
 => "01234" 
Run Code Online (Sandbox Code Playgroud)

请参阅Regexp.last_matchMatchData