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
stringfor embedding in JavaScript source, using the default options. - jsesc_
with - Escape
stringwith the givenOptions.