From: Ammar Ali Date: 2012-04-01T04:53:08+09:00 Subject: Re: regexp replace every other one On Sat, Mar 31, 2012 at 10:17 PM, gabe gabriellini wrote: > puts "Mike Lan move the string abc def".gsub(/(\A|\s)(.{3})($|\s)/," > new_value ") > > it only replaces two same as the next one only replaces every other one > what a im missing?? The ending pattern ($|\s) is consuming what would be matched by the leading pattern (\A|\s) every other match. Making the first part optional will work: /(\A|\s)?(.{3})($|\s)/ If you know the pattern of what you trying to match between the spaces, for example alphabet only, use a more explicit pattern instead of .{3}. That way you wouldn't have to deal with the spaces, only what you want to replace. >> "abc def ghi jkl mno".gsub(/[a-z]{3}/, "123") => "123 123 123 123 123" Regards, Ammar