Skip to main content

Crate jsesc

Crate jsesc 

Source
Expand description

§jsesc — escape a string for JavaScript or JSON

Escape a string so it can be safely embedded in JavaScript or JSON source — handling quotes, backslashes, control characters, line/paragraph separators, and (optionally) every non-ASCII character. A faithful Rust port of the string-escaping of the widely-used jsesc npm package by Mathias Bynens.

use jsesc::jsesc;

assert_eq!(jsesc("café"), "caf\\xE9");
assert_eq!(jsesc("foo'bar"), "foo\\'bar");

Use jsesc_with / Options for double/backtick quotes, wrapping, JSON mode, ES6 unicode escapes, ASCII-only output, and more:

use jsesc::{jsesc_with, Options, Quotes};

assert_eq!(jsesc_with("hi", &Options::new().json(true)), "\"hi\"");
assert_eq!(jsesc_with("ab", &Options::new().escape_everything(true)), "\\x61\\x62");

Zero dependencies and #![no_std].

Structs§

Options
Options for jsesc_with.

Enums§

Quotes
The quote style to escape for (and wrap with).

Functions§

jsesc
Escape string for embedding in JavaScript source, using the default options.
jsesc_with
Escape string with the given Options.