js-string-escape
Escape strings for use as JavaScript string literals
Last updated 4 years ago by joliss .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install js-string-escape 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 1.0.1                                ...           latest (4 years ago)

3 Versions

  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           7 years ago
  • 0.0.1                                ...           7 years ago
Maintainers (1)
Downloads
Today 122
This Week 4,005
This Month 15,637
Last Day 695
Last Week 4,043
Last Month 14,570
Dependencies (0)
None
Dev Dependencies (2)
Dependents (289)

Copyright 2014 - 2016 © taobao.org |