Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

Regexp.quote

(::interned str) -> ::String
Regexp.escape(string) → new_string

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 "\\\\\\*\\?\\{\\}\\.">
See also .new .timeout .linear_time? .timeout= also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0