Ruby doesn't recognize g flag for regular expression

Is this implied by default in str.scan? By default, is this disabled in str [regex]?

+4
source share
2 answers

Yes, how often a regular expression is used depends on the method used, and not on the flags of regular expressions.

scan returns an array containing (or iterating over) all regular expression matches. match and String#[] will return the first match. =~ will return the index of the first match. gsub will replace all occurrences of the regex, and sub will replace the first occurrence.

+7
source
 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" 
+3
source

Source: https://habr.com/ru/post/1301759/


All Articles