jquery – I need to escape double quotes in javascript

jquery – I need to escape double quotes in javascript

You must escape the double quotes manually. Theres no way to do automatically because the str produce the following error then javascript wouldnt execute further ahead lines of code:

enter

Use the backslash ( ) to escape your double quotes.

It should look like this :

var str = (this string has double quotes as well as single quotes..);

Theres no automatic way to do this.

jquery – I need to escape double quotes in javascript

If you want to replace the quotes, it is best to replace them with their HTML entity. Using a method like this, you can accomplish it.

var tokens = [
        [, "],
        [, ']
];
var clean = [];

for(var i = 0; i < MY_STRING.length; i++) {     
  var s = MY_STRING[i];
  for(var a = 0; a < tokens.length; a++)
    if(tokens[a][0] == s) {
      s = tokens[a][1];
      break;
    }
    clean.push(s);                      
}
var cleanString = clean.join();

Leave a Reply

Your email address will not be published. Required fields are marked *