Created
January 24, 2012 01:05
-
-
Save JEG2/1667098 to your computer and use it in GitHub Desktop.
An attempt to escape content for a regular expression.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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