From: Matthew Kerwin Date: 2012-10-18T09:51:47+09:00 Subject: Re: Drop 1st and last particular character Tangentially, it just occurred to me that ruby's regular expression engine does the same thing that javascript's does, when globally replacing /X*$/ . It arose when someone wanted to replace any number (or none) of a character at the start and end of a string with exactly one of that character. irb(main):001:0> 'foo'.gsub(/\A#*|#*\Z/, '#') => "#foo#" irb(main):002:0> '#foo'.gsub(/\A#*|#*\Z/, '#') => "#foo#" irb(main):003:0> '##foo'.gsub(/\A#*|#*\Z/, '#') => "#foo#" irb(main):004:0> 'foo#'.gsub(/\A#*|#*\Z/, '#') => "#foo##" irb(main):005:0> 'foo##'.gsub(/\A#*|#*\Z/, '#') => "#foo##" irb(main):006:0> '##foo##'.gsub(/\A#*|#*\Z/, '#') => "#foo##" The original javascript question came up on StackOverflow I blogged about it here: http://matthew.kerwin.net.au/blog/20110608_javascript_global_regexp -- Matthew Kerwin, B.Sc (CompSci) (Hons) http://matthew.kerwin.net.au/ ABN: 59-013-727-651 "You'll never find a programming language that frees you from the burden of clarifying your ideas." - xkcd