javascript escape characters code example

Example 1: javascript escape html

function escapeHtml(str) {
    return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
}

Example 2: js escape characters

JS Escape Characters
\' — Single quote
\" — Double quote
\\ — Backslash
\b — Backspace
\f — Form feed
\n — New line
\r — Carriage return
\t — Horizontal tabulator
WebsiteSetup.org - Beginner’s Javascript Cheat Sheet 7
\v — Vertical tabulator

Example 3: js json escape

JSON.parse(JSON.stringify(jsonObject));

Example 4: escape in javascript

escape sequences in javascript
\' single quote
\" doucle quote
\\ backslash
\n newline
\r carriage return
\t tab
\b backspace
\f form feed

Example 5: add a slash to string in javascript

var str = 'USDYEN'
// add a / in between currencies
// var newStr = str.slice(0, 3) + ' / ' + str.slice(3)

// var newStr = str.slice(3) // removes the first 3 chars
// var newStr = str.slice(0,3) // removes the last 3 chars
var newStr = str.slice(0,3) + ' / ' + str.slice(3) // removes the first 3 and adds the last 3

console.log(newStr)
// => USD / YEN