咸鱼要翻身
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.
 
 
 
 
 
yangqiwen e9ed3cf0b9 咸鱼要翻身 2 months ago
..
CHANGELOG.md 咸鱼要翻身 2 months ago
LICENSE 咸鱼要翻身 2 months ago
README.md 咸鱼要翻身 2 months ago
index.js 咸鱼要翻身 2 months ago
package.json 咸鱼要翻身 2 months ago

README.md

js-string-escape

Build Status

Escape any string to be a valid JavaScript string literal between double quotes or single quotes.

Installation

npm install js-string-escape

Example

If you need to generate JavaScript output, this library will help you safely put arbitrary data in JavaScript strings:

jsStringEscape = require('js-string-escape')

console.log('"' + jsStringEscape('Quotes (\", \'), newlines (\n), etc.') + '"')
// => "Quotes (\", \'), newlines (\n), etc."

In other words, given any string s, the following invariants hold:

eval('"' + jsStringEscape(s) + '"') === s
eval("'" + jsStringEscape(s) + "'") === s

These eval expressions are safe with untrusted strings s.

Non-strings will be cast to strings.

Compliance

This library has been checked against ECMAScript 5.1 and tested against all Unicode code points.

Note that the returned string is not necessarily valid JSON, since JSON disallows control characters, and \' is illegal in JSON.