This results from the interaction between a couple of regex features and Unicode. \W
is all non-word characters, which includes 212A - "KELVIN SIGN" K
(PDF link) and 017F - "LATIN SMALL LETTER LONG S" ſ
(PDF link). The /i
adds lower case versions of both of these, which are the “normal” k
and s
characters (006B - "LATIN SMALL LETTER K" and 0073 "LATIN SMALL LETTER S" (PDF link)).
So it’s the s
in password
that’s being interpreted as a non-word character in certain cases.
Note that this only seems to occur when the \W
is in a character class (i.e. [\W]
). Also I can only reproduce this in irb
, inside a standalone script it seems to work as expected.
See the Ruby bug about this for more information.