Skip to content

Instantly share code, notes, and snippets.

@JEG2
Created January 24, 2012 01:05
Show Gist options
  • Select an option

  • Save JEG2/1667098 to your computer and use it in GitHub Desktop.

Select an option

Save JEG2/1667098 to your computer and use it in GitHub Desktop.
An attempt to escape content for a regular expression.
(defun jeg2s-regex-escape (str)
"Escape all special regular expression characters in the passed string."
(if (stringp str)
(jeg2s-regex-escape (cons "" (split-string str "" t)))
(if (>= (safe-length str) 2)
(if (and (not (string= "\\" (car str))) (cadr (string-match "[.*+?^$\\\\[]" str)))
(concat (car str) "\\" (cadr str) (jeg2s-regex-escape (cddr str)))
(concat (car str) (jeg2s-regex-escape (cdr str)))
(mapconcat 'identity str "")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment