Skip to content
class method

Regexp.escape

(::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 .compile .json_create .last_match also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0