1. js-string-escape
Escape strings for use as JavaScript string literals
js-string-escape
Package: js-string-escape
Created by: joliss
Last modified: Sun, 19 Jun 2022 05:44:07 GMT
Version: 1.0.1
License: MIT
Downloads: 13,085,414
Repository: https://github.com/joliss/js-string-escape

Install

npm install js-string-escape
yarn add js-string-escape

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.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code