jsesc
Escape a string for safe embedding in JavaScript or JSON source.
Handles quotes, backslashes, control characters, line/paragraph separators (U+2028 /
U+2029), and — optionally — every non-ASCII character. A faithful Rust port of the string
escaping of the widely-used jsesc npm package by
Mathias Bynens.
- Zero dependencies,
#![no_std] - Single / double / backtick quotes, wrapping, JSON mode, ES6 unicode escapes, ASCII-only
output, minimal escaping, and
<script>-context safety - Differential-tested against the reference
jsescimplementation (60k cases, all options)
Install
[]
= "0.1"
Usage
use ;
assert_eq!;
assert_eq!;
// JSON-safe (double quotes, wrapped, \uXXXX escapes):
assert_eq!;
// ASCII-only output, ES6 astral escapes, backtick quotes:
assert_eq!;
assert_eq!;
assert_eq!;
Scope
This crate ports jsesc's string escaping, which is its primary use. The npm package can
also serialize numbers, arrays, and objects; for that, use serde_json.
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
Licensed under either of MIT or Apache-2.0 at your option.