javascript escape single quote code example

Example 1: javascript remove quotes from string

var someStr = 'He said "Hello, my name is Foo"';
console.log(someStr.replace(/['"]+/g, ''));

Example 2: javascript escape quote method

stringToEscape.replace(/"/g, '\\\"')

Example 3: javascript escape single quote

document.getElementById("something").innerHTML = "<img src='something' onmouseover='change(\"ex1\")' />";

Example 4: html escape function javascript

function escapeHTML(text) {  
    var replacements= {"<": "&lt;", ">": "&gt;","&": "&amp;", """: "&quot;"};                      
    return text.replace(/[<>&"]/g, function(character) {  
        return replacements[character];  
    }); 
}

Example 5: javascript escape single quote

var string = 'this isn\'t a double quoted string';
var string = "this isn\"t a single quoted string";
//           ^         ^ same types, hence we need to escape it with a backslash