Addslashes and stripslashes in Javascript

Simple one. If you need to add/remove escape slashes to/from a string, use the following code.

[source language=“php“]
function addslashes(str) {
str = str.replace(/\\/g, ‚\\\\‘);
str = str.replace(/\’/g, ‚\\\“);
str = str.replace(/\"/g, ‚\\"‘);
str = str.replace(/\0/g, ‚\\0‘);
return str;
}

function stripslashes(str) {
str = str.replace(/\\’/g, ‚\“);
str = str.replace(/\\"/g, ‚"‘);
str = str.replace(/\\0/g, ‚\0‘);
str = str.replace(/\\\\/g, ‚\\‘);
return str;
}
[/source]

Source – http://javascript.about.com/library/bladdslash.htm

Ein Kommentar zu „Addslashes and stripslashes in Javascript

Kommentar verfassen