Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
Regexp.quote
(::interned str) -> ::String
Regexp.escape(string) → new_string
re.c
View on GitHub ↗
static VALUE
rb_reg_s_quote(VALUE c, VALUE str)
{
return rb_reg_quote(reg_operand(str, TRUE));
}
Returns a new string that escapes any characters that have special meaning in a regular expression:
Example
s = Regexp.escape('\*?{}.') # => "\\\\\\*\\?\\{\\}\\."
For any string s, this call returns a MatchData object:
Example
r = Regexp.new(Regexp.escape(s)) # => /\\\\\\\*\\\?\\\{\\\}\\\./ r.match(s) # => #<MatchData "\\\\\\*\\?\\{\\}\\.">