Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Escape an string for using with a Regex constructor
function sanitizeForRegex (str) {
return String(str).replace(/(?=[.?*+^$[\]\\(){}|-])/g, '\\')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment