End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
Regexp.escape
Regexp.escape(str) → string
Regexp.quote(str) → string
re.c
View on GitHub ↗
static VALUE
rb_reg_s_quote(VALUE c, VALUE str)
{
return rb_reg_quote(reg_operand(str, TRUE));
}
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('\*?{}.') #=> \\\*\?\{\}\.