Skip to content

Instantly share code, notes, and snippets.

@robozevel
Created April 2, 2014 13:18
Show Gist options
  • Save robozevel/9933901 to your computer and use it in GitHub Desktop.
Save robozevel/9933901 to your computer and use it in GitHub Desktop.
Escape SQL string (extracted from node-mysql)
// https://github.com/felixge/node-mysql/blob/master/lib/protocol/SqlString.js
function escapeSqlString(text) {
return text.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, function(s) {
switch(s) {
case "\0" : return "\\0";
case "\n" : return "\\n";
case "\r" : return "\\r";
case "\b" : return "\\b";
case "\t" : return "\\t";
case "\x1a": return "\\Z";
default : return "\\" + s;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment