How to Properly Escape Quotes

CSS

vat strin

JavaScript

/*

How to Properly Escape Quotes
-----------------------------

Quotes need to be "escaped" in certain cases so the machine knows not to end the quote.
Quotes inside of quotes are only acceptable in the following cases:

- Single quotes are allowed inside double quotes all the time
  Ex: "This is 'totally' acceptable"

- Double quotes are allowed inside single quotes all the time
  Ex: 'This is also acceptable, "dude"'

- Single quotes inside of single quotes need to be escaped
  Ex: 'This is acceptable if they are \'escaped\' with a backslash'

- Double quotes inside of double quotes need to be escaped
  Ex: "This is also acceptable if they are \"escaped\" with a backslash"



Here's all of the examples next to each other:

// Acceptable
"This is 'totally' acceptable"
'This is also acceptable, "dude"'
'This is acceptable if they are \'escaped\' with a backslash'
"This is also acceptable if they are \"escaped\" with a backslash"

// Unacceptable
"This is "totally" NOT acceptable"
'This is also NOT acceptable, 'dude''













*/