Ruby regex what does the \1 mean for gsub

后端 未结 3 2047
一生所求
一生所求 2021-02-01 08:20

What does the \\1 do?

For example

\"foo bar bag\".gsub(/(bar)/,\'car\\1\')

I believe it has something to do with how you use parenthese

3条回答
  •  醉话见心
    2021-02-01 08:54

    The \1 is syntax for the last capture in a regular expression using the () like you said. It says whatever was matched, replace it with that.

    You can continually use () groups and their respective \2 to continue to replace what you matched.

提交回复
热议问题