You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
644 B
23 lines
644 B
4 weeks ago
|
module.exports = function (string) {
|
||
|
return ('' + string).replace(/["'\\\n\r\u2028\u2029]/g, function (character) {
|
||
|
// Escape all characters not included in SingleStringCharacters and
|
||
|
// DoubleStringCharacters on
|
||
|
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
|
||
|
switch (character) {
|
||
|
case '"':
|
||
|
case "'":
|
||
|
case '\\':
|
||
|
return '\\' + character
|
||
|
// Four possible LineTerminator characters need to be escaped:
|
||
|
case '\n':
|
||
|
return '\\n'
|
||
|
case '\r':
|
||
|
return '\\r'
|
||
|
case '\u2028':
|
||
|
return '\\u2028'
|
||
|
case '\u2029':
|
||
|
return '\\u2029'
|
||
|
}
|
||
|
})
|
||
|
}
|