What does \1 (backslash 1) do in Ruby strings? -


activesupport::inflector::inflections#human uses \1 in example:

human /_cnt$/i, '\1_count' 

as far can tell, that's adding \u0001 character string, , seems still work if use '_count'. can tell me \1 supposed doing?

it may under hood, passing string gsub or similar. in gsub replacement string, '\1' has special meaning -- refers whatever first capturing group in regex argument matched.

for example, try:

 "a short sentence".gsub(/([aeiou])/, '\1\1') 

if use "\1", different thing. \u001 character. (escapes work differently in single-quoted , double-quoted ruby strings.)

probably reason why omitting \1 doesn't seem change in example gave, because there no capturing group in regex.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -