Expand description
§js-string-escape — escape a string for a JavaScript string literal
Escape the characters that aren’t valid inside a single- or double-quoted JavaScript
string literal — ", ', \, and the line terminators \n, \r, U+2028, and
U+2029. Everything else (including tabs, other control characters, and non-ASCII) is
left as-is. A faithful Rust port of the
js-string-escape npm package.
use js_string_escape::js_string_escape;
assert_eq!(js_string_escape(r#"a"b'c\d"#), r#"a\"b\'c\\d"#);
assert_eq!(js_string_escape("line1\nline2"), "line1\\nline2");
assert_eq!(js_string_escape("café\t"), "café\t"); // non-ASCII and tab pass throughThis is the minimal escaping needed to embed arbitrary text into a JS string literal; for
richer options (ASCII-only output, JSON mode, …) see
jsesc.
Zero dependencies and #![no_std].
Functions§
- js_
string_ escape - Escape
stringso it can be embedded in a JavaScript string literal.