Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

Regexp.quote

Regexp.escape(str) → string
Regexp.quote(str) → string

Escapes any characters that would have special meaning in a regular expression. Returns a new escaped string with the same or compatible encoding. For any string, Regexp.new(Regexp.escape(<i>str</i>))=~<i>str</i> will be true.

Example
Regexp.escape('\*?{}.')   #=> \\\*\?\{\}\.
See also .new .try_convert .last_match .union also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1