Function: regexp-quote

regexp-quote is a function defined in search.c.

Signature

(regexp-quote STRING)

Documentation

Return a regexp string which matches exactly STRING and nothing else.

Other relevant functions are documented in the regexp group.

View in manual

Probably introduced at or before Emacs version 21.1.

Shortdoc

;; regexp
(regexp-quote "foo.*bar")
    => "foo\\.\\*bar"

Source Code

// Defined in /usr/src/emacs/src/search.c
{
  char *in, *out, *end;
  char *temp;
  ptrdiff_t backslashes_added = 0;

  CHECK_STRING (string);

  USE_SAFE_ALLOCA;
  SAFE_NALLOCA (temp, 2, SBYTES (string));

  /* Now copy the data into the new string, inserting escapes. */

  in = SSDATA (string);
  end = in + SBYTES (string);
  out = temp;

  for (; in != end; in++)
    {
      if (*in == '['
	  || *in == '*' || *in == '.' || *in == '\\'
	  || *in == '?' || *in == '+'
	  || *in == '^' || *in == '$')
	*out++ = '\\', backslashes_added++;
      *out++ = *in;
    }

  Lisp_Object result
    = (backslashes_added > 0
       ? make_specified_string (temp,
                                SCHARS (string) + backslashes_added,
                                out - temp,
                                STRING_MULTIBYTE (string))
       : string);
  SAFE_FREE ();
  return result;
}