Created
August 15, 2014 20:20
-
-
Save arolle/23627fb2a878060b6b63 to your computer and use it in GitHub Desktop.
protects all regexp special characters
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
(: | |
: protects all regexp special characters | |
: @author Marc van Grootel | |
: @see http://www.mail-archive.com/[email protected]/msg04573.html | |
:) | |
declare function route:re-escape($string as xs:string) | |
as xs:string { | |
let $regexp-chars := ('.','*','+','|','?','(',')','[',']','{','}','^') | |
return | |
(: Note that '\' and '$' in the fold caused invalid pattern errors therefore put them in separate replace :) | |
fold-left( | |
$regexp-chars, | |
replace(replace($string, '\\', '\\\\'), '\$', '\\\$'), | |
function($a, $b) { | |
replace($a, '\'||$b, '\\'||$b ) | |
} | |
) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment