1. Home
  2. Computing & Technology
  3. JavaScript

Addslashes and Stripslashes

clr gif
Join the Discussion

Questions? Comments?

If you are used to programming your server side code using PHP then you have probably used the addslashes() and stripslashes() functions to handle the processing of certain characters such as quotes within input fields. Javascript doesn't come with equivalent functions but you can easily add these functions into Javascript by adding the following code:

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;
}
Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.