This is a Java\+Script library for \href{http://mathiasbynens.be/notes/javascript-escapes}{\texttt{ escaping Java\+Script strings}} while generating the shortest possible valid ASCII-\/only output. \href{http://mothereff.in/js-escapes}{\texttt{ Here’s an online demo.}}

This can be used to avoid \href{http://en.wikipedia.org/wiki/Mojibake}{\texttt{ mojibake}} and other encoding issues, or even to \href{https://twitter.com/annevk/status/380000829643571200}{\texttt{ avoid errors}} when passing JSON-\/formatted data (which may contain U+2028 LINE SEPARATOR, U+2029 PARAGRAPH SEPARATOR, or \href{http://esdiscuss.org/topic/code-points-vs-unicode-scalar-values\#content-14}{\texttt{ lone surrogates}}) to a Java\+Script parser or an UTF-\/8 encoder, respectively.

Feel free to fork if you see possible improvements!\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24133}{}\doxysection{Installation}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24133}
Via \href{http://bower.io/}{\texttt{ Bower}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{bower install jsesc }

\end{DoxyCode}


Via \href{https://github.com/component/component}{\texttt{ Component}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{component install mathiasbynens/jsesc }

\end{DoxyCode}


Via \href{http://npmjs.org/}{\texttt{ npm}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{npm install jsesc }

\end{DoxyCode}


In a browser\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{<script src="{}jsesc.js"{}></script> }

\end{DoxyCode}


In \href{http://nodejs.org/}{\texttt{ Node.\+js}} and \href{http://ringojs.org/}{\texttt{ Ringo\+JS}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var jsesc = require('jsesc'); }

\end{DoxyCode}


In \href{http://narwhaljs.org/}{\texttt{ Narwhal}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var jsesc = require('jsesc').jsesc; }

\end{DoxyCode}


In \href{http://www.mozilla.org/rhino/}{\texttt{ Rhino}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{load('jsesc.js'); }

\end{DoxyCode}


Using an AMD loader like \href{http://requirejs.org/}{\texttt{ Require\+JS}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{require(}
\DoxyCodeLine{  \{}
\DoxyCodeLine{    'paths': \{}
\DoxyCodeLine{      'jsesc': 'path/to/jsesc'}
\DoxyCodeLine{    \}}
\DoxyCodeLine{  \},}
\DoxyCodeLine{  ['jsesc'],}
\DoxyCodeLine{  function(jsesc) \{}
\DoxyCodeLine{    console.log(jsesc);}
\DoxyCodeLine{  \}}
\DoxyCodeLine{); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24134}{}\doxysection{API}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24134}
\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24135}{}\doxysubsection{$<$tt$>$jsesc(value, options)$<$/tt$>$}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24135}
This function takes a value and returns an escaped version of the value where any characters that are not printable ASCII symbols are escaped using the shortest possible (but valid) \href{http://mathiasbynens.be/notes/javascript-escapes}{\texttt{ escape sequences for use in Java\+Script strings}}. The first supported value type is strings\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{jsesc('Ich ♥ Bücher');}
\DoxyCodeLine{// → 'Ich \(\backslash\)\(\backslash\)u2665 B\(\backslash\)\(\backslash\)xFCcher'}
\DoxyCodeLine{}
\DoxyCodeLine{jsesc('foo 𝌆 bar');}
\DoxyCodeLine{// → 'foo \(\backslash\)\(\backslash\)uD834\(\backslash\)\(\backslash\)uDF06 bar' }

\end{DoxyCode}


Instead of a string, the {\ttfamily value} can also be an array, or an object. In such cases, {\ttfamily jsesc} will return a stringified version of the value where any characters that are not printable ASCII symbols are escaped in the same way.


\begin{DoxyCode}{0}
\DoxyCodeLine{// Escaping an array}
\DoxyCodeLine{jsesc([}
\DoxyCodeLine{  'Ich ♥ Bücher', 'foo 𝌆 bar'}
\DoxyCodeLine{]);}
\DoxyCodeLine{// → '[\(\backslash\)'Ich \(\backslash\)\(\backslash\)u2665 B\(\backslash\)\(\backslash\)xFCcher\(\backslash\)',\(\backslash\)'foo \(\backslash\)\(\backslash\)uD834\(\backslash\)\(\backslash\)uDF06 bar\(\backslash\)']'}
\DoxyCodeLine{}
\DoxyCodeLine{// Escaping an object}
\DoxyCodeLine{jsesc(\{}
\DoxyCodeLine{  'Ich ♥ Bücher': 'foo 𝌆 bar'}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '\{\(\backslash\)'Ich \(\backslash\)\(\backslash\)u2665 B\(\backslash\)\(\backslash\)xFCcher\(\backslash\)':\(\backslash\)'foo \(\backslash\)\(\backslash\)uD834\(\backslash\)\(\backslash\)uDF06 bar\(\backslash\)'\}' }

\end{DoxyCode}


The optional {\ttfamily options} argument accepts an object with the following options\+:\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24136}{}\doxysubsubsection{$<$tt$>$quotes$<$/tt$>$}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24136}
The default value for the {\ttfamily quotes} option is `\textquotesingle{}single'{\ttfamily . This means that any occurences of}\textquotesingle{}{\ttfamily in the input string will be escaped as}\textbackslash{}\textquotesingle{}\`{}, so that the output can be used in a string literal wrapped in single quotes.


\begin{DoxyCode}{0}
\DoxyCodeLine{jsesc('Lorem ipsum "{}dolor"{} sit \(\backslash\)'amet\(\backslash\)' etc.');}
\DoxyCodeLine{// → 'Lorem ipsum "{}dolor"{} sit \(\backslash\)\(\backslash\)\(\backslash\)'amet\(\backslash\)\(\backslash\)\(\backslash\)' etc.'}
\DoxyCodeLine{}
\DoxyCodeLine{jsesc('Lorem ipsum "{}dolor"{} sit \(\backslash\)'amet\(\backslash\)' etc.', \{}
\DoxyCodeLine{  'quotes': 'single'}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → 'Lorem ipsum "{}dolor"{} sit \(\backslash\)\(\backslash\)\(\backslash\)'amet\(\backslash\)\(\backslash\)\(\backslash\)' etc.'}
\DoxyCodeLine{// → "{}Lorem ipsum \(\backslash\)"{}dolor\(\backslash\)"{} sit \(\backslash\)\(\backslash\)'amet\(\backslash\)\(\backslash\)' etc."{} }

\end{DoxyCode}


If you want to use the output as part of a string literal wrapped in double quotes, set the {\ttfamily quotes} option to `\textquotesingle{}double'\`{}.


\begin{DoxyCode}{0}
\DoxyCodeLine{jsesc('Lorem ipsum "{}dolor"{} sit \(\backslash\)'amet\(\backslash\)' etc.', \{}
\DoxyCodeLine{  'quotes': 'double'}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → 'Lorem ipsum \(\backslash\)\(\backslash\)"{}dolor\(\backslash\)\(\backslash\)"{} sit \(\backslash\)'amet\(\backslash\)' etc.'}
\DoxyCodeLine{// → "{}Lorem ipsum \(\backslash\)\(\backslash\)\(\backslash\)"{}dolor\(\backslash\)\(\backslash\)\(\backslash\)"{} sit 'amet' etc."{} }

\end{DoxyCode}


This setting also affects the output for arrays and objects\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{jsesc(\{ 'Ich ♥ Bücher': 'foo 𝌆 bar' \}, \{}
\DoxyCodeLine{  'quotes': 'double'}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '\{"{}Ich \(\backslash\)\(\backslash\)u2665 B\(\backslash\)\(\backslash\)xFCcher"{}:"{}foo \(\backslash\)\(\backslash\)uD834\(\backslash\)\(\backslash\)uDF06 bar"{}\}'}
\DoxyCodeLine{}
\DoxyCodeLine{jsesc([ 'Ich ♥ Bücher', 'foo 𝌆 bar' ], \{}
\DoxyCodeLine{  'quotes': 'double'}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '["{}Ich \(\backslash\)\(\backslash\)u2665 B\(\backslash\)\(\backslash\)xFCcher"{},"{}foo \(\backslash\)\(\backslash\)uD834\(\backslash\)\(\backslash\)uDF06 bar"{}]' }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24137}{}\doxysubsubsection{$<$tt$>$wrap$<$/tt$>$}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24137}
The {\ttfamily wrap} option takes a boolean value ({\ttfamily true} or {\ttfamily false}), and defaults to {\ttfamily false} (disabled). When enabled, the output will be a valid Java\+Script string literal wrapped in quotes. The type of quotes can be specified through the {\ttfamily quotes} setting.


\begin{DoxyCode}{0}
\DoxyCodeLine{jsesc('Lorem ipsum "{}dolor"{} sit \(\backslash\)'amet\(\backslash\)' etc.', \{}
\DoxyCodeLine{  'quotes': 'single',}
\DoxyCodeLine{  'wrap': true}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '\(\backslash\)'Lorem ipsum "{}dolor"{} sit \(\backslash\)\(\backslash\)\(\backslash\)'amet\(\backslash\)\(\backslash\)\(\backslash\)' etc.\(\backslash\)''}
\DoxyCodeLine{// → "{}\(\backslash\)'Lorem ipsum \(\backslash\)"{}dolor\(\backslash\)"{} sit \(\backslash\)\(\backslash\)\(\backslash\)'amet\(\backslash\)\(\backslash\)\(\backslash\)' etc.\(\backslash\)'"{}}
\DoxyCodeLine{}
\DoxyCodeLine{jsesc('Lorem ipsum "{}dolor"{} sit \(\backslash\)'amet\(\backslash\)' etc.', \{}
\DoxyCodeLine{  'quotes': 'double',}
\DoxyCodeLine{  'wrap': true}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '"{}Lorem ipsum \(\backslash\)\(\backslash\)"{}dolor\(\backslash\)\(\backslash\)"{} sit \(\backslash\)'amet\(\backslash\)' etc."{}'}
\DoxyCodeLine{// → "{}\(\backslash\)"{}Lorem ipsum \(\backslash\)\(\backslash\)\(\backslash\)"{}dolor\(\backslash\)\(\backslash\)\(\backslash\)"{} sit \(\backslash\)'amet\(\backslash\)' etc.\(\backslash\)"{}"{} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24138}{}\doxysubsubsection{$<$tt$>$es6$<$/tt$>$}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24138}
The {\ttfamily es6} option takes a boolean value ({\ttfamily true} or {\ttfamily false}), and defaults to {\ttfamily false} (disabled). When enabled, any astral Unicode symbols in the input will be escaped using \href{http://mathiasbynens.be/notes/javascript-escapes\#unicode-code-point}{\texttt{ ECMAScript 6 Unicode code point escape sequences}} instead of using separate escape sequences for each surrogate half. If backwards compatibility with ES5 environments is a concern, don’t enable this setting. If the {\ttfamily json} setting is enabled, the value for the {\ttfamily es6} setting is ignored (as if it was {\ttfamily false}).


\begin{DoxyCode}{0}
\DoxyCodeLine{// By default, the `es6` option is disabled:}
\DoxyCodeLine{jsesc('foo 𝌆 bar 💩 baz');}
\DoxyCodeLine{// → 'foo \(\backslash\)\(\backslash\)uD834\(\backslash\)\(\backslash\)uDF06 bar \(\backslash\)\(\backslash\)uD83D\(\backslash\)\(\backslash\)uDCA9 baz'}
\DoxyCodeLine{}
\DoxyCodeLine{// To explicitly disable it:}
\DoxyCodeLine{jsesc('foo 𝌆 bar 💩 baz', \{}
\DoxyCodeLine{  'es6': false}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → 'foo \(\backslash\)\(\backslash\)uD834\(\backslash\)\(\backslash\)uDF06 bar \(\backslash\)\(\backslash\)uD83D\(\backslash\)\(\backslash\)uDCA9 baz'}
\DoxyCodeLine{}
\DoxyCodeLine{// To enable it:}
\DoxyCodeLine{jsesc('foo 𝌆 bar 💩 baz', \{}
\DoxyCodeLine{  'es6': true}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → 'foo \(\backslash\)\(\backslash\)u\{1D306\} bar \(\backslash\)\(\backslash\)u\{1F4A9\} baz' }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24139}{}\doxysubsubsection{$<$tt$>$escape\+Everything$<$/tt$>$}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24139}
The {\ttfamily escape\+Everything} option takes a boolean value ({\ttfamily true} or {\ttfamily false}), and defaults to {\ttfamily false} (disabled). When enabled, all the symbols in the output will be escaped, even printable ASCII symbols.


\begin{DoxyCode}{0}
\DoxyCodeLine{jsesc('lolwat"{}foo\(\backslash\)'bar', \{}
\DoxyCodeLine{  'escapeEverything': true}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '\(\backslash\)\(\backslash\)x6C\(\backslash\)\(\backslash\)x6F\(\backslash\)\(\backslash\)x6C\(\backslash\)\(\backslash\)x77\(\backslash\)\(\backslash\)x61\(\backslash\)\(\backslash\)x74\(\backslash\)\(\backslash\)"{}\(\backslash\)\(\backslash\)x66\(\backslash\)\(\backslash\)x6F\(\backslash\)\(\backslash\)x6F\(\backslash\)\(\backslash\)\(\backslash\)'\(\backslash\)\(\backslash\)x62\(\backslash\)\(\backslash\)x61\(\backslash\)\(\backslash\)x72'}
\DoxyCodeLine{// → "{}\(\backslash\)\(\backslash\)x6C\(\backslash\)\(\backslash\)x6F\(\backslash\)\(\backslash\)x6C\(\backslash\)\(\backslash\)x77\(\backslash\)\(\backslash\)x61\(\backslash\)\(\backslash\)x74\(\backslash\)\(\backslash\)\(\backslash\)"{}\(\backslash\)\(\backslash\)x66\(\backslash\)\(\backslash\)x6F\(\backslash\)\(\backslash\)x6F\(\backslash\)\(\backslash\)'\(\backslash\)\(\backslash\)x62\(\backslash\)\(\backslash\)x61\(\backslash\)\(\backslash\)x72"{} }

\end{DoxyCode}


This setting also affects the output for arrays and objects\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{jsesc(\{ 'Ich ♥ Bücher': 'foo 𝌆 bar' \}, \{}
\DoxyCodeLine{  'escapeEverything': true}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '\{\(\backslash\)'\(\backslash\)x49\(\backslash\)x63\(\backslash\)x68\(\backslash\)x20\(\backslash\)u2665\(\backslash\)x20\(\backslash\)x42\(\backslash\)xFC\(\backslash\)x63\(\backslash\)x68\(\backslash\)x65\(\backslash\)x72\(\backslash\)':\(\backslash\)'\(\backslash\)x66\(\backslash\)x6F\(\backslash\)x6F\(\backslash\)x20\(\backslash\)uD834\(\backslash\)uDF06\(\backslash\)x20\(\backslash\)x62\(\backslash\)x61\(\backslash\)x72\(\backslash\)'\}'}
\DoxyCodeLine{// → "{}\{'\(\backslash\)x49\(\backslash\)x63\(\backslash\)x68\(\backslash\)x20\(\backslash\)u2665\(\backslash\)x20\(\backslash\)x42\(\backslash\)xFC\(\backslash\)x63\(\backslash\)x68\(\backslash\)x65\(\backslash\)x72':'\(\backslash\)x66\(\backslash\)x6F\(\backslash\)x6F\(\backslash\)x20\(\backslash\)uD834\(\backslash\)uDF06\(\backslash\)x20\(\backslash\)x62\(\backslash\)x61\(\backslash\)x72'\}"{}}
\DoxyCodeLine{}
\DoxyCodeLine{jsesc([ 'Ich ♥ Bücher': 'foo 𝌆 bar' ], \{}
\DoxyCodeLine{  'escapeEverything': true}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '[\(\backslash\)'\(\backslash\)x49\(\backslash\)x63\(\backslash\)x68\(\backslash\)x20\(\backslash\)u2665\(\backslash\)x20\(\backslash\)x42\(\backslash\)xFC\(\backslash\)x63\(\backslash\)x68\(\backslash\)x65\(\backslash\)x72\(\backslash\)',\(\backslash\)'\(\backslash\)x66\(\backslash\)x6F\(\backslash\)x6F\(\backslash\)x20\(\backslash\)uD834\(\backslash\)uDF06\(\backslash\)x20\(\backslash\)x62\(\backslash\)x61\(\backslash\)x72\(\backslash\)']' }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24140}{}\doxysubsubsection{$<$tt$>$compact$<$/tt$>$}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24140}
The {\ttfamily compact} option takes a boolean value ({\ttfamily true} or {\ttfamily false}), and defaults to {\ttfamily true} (enabled). When enabled, the output for arrays and objects will be as compact as possible; it won’t be formatted nicely.


\begin{DoxyCode}{0}
\DoxyCodeLine{jsesc(\{ 'Ich ♥ Bücher': 'foo 𝌆 bar' \}, \{}
\DoxyCodeLine{  'compact': true // this is the default}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '\{\(\backslash\)'Ich \(\backslash\)u2665 B\(\backslash\)xFCcher\(\backslash\)':\(\backslash\)'foo \(\backslash\)uD834\(\backslash\)uDF06 bar\(\backslash\)'\}'}
\DoxyCodeLine{}
\DoxyCodeLine{jsesc(\{ 'Ich ♥ Bücher': 'foo 𝌆 bar' \}, \{}
\DoxyCodeLine{  'compact': false}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '\{\(\backslash\)n\(\backslash\)t\(\backslash\)'Ich \(\backslash\)u2665 B\(\backslash\)xFCcher\(\backslash\)': \(\backslash\)'foo \(\backslash\)uD834\(\backslash\)uDF06 bar\(\backslash\)'\(\backslash\)n\}'}
\DoxyCodeLine{}
\DoxyCodeLine{jsesc([ 'Ich ♥ Bücher', 'foo 𝌆 bar' ], \{}
\DoxyCodeLine{  'compact': false}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '[\(\backslash\)n\(\backslash\)t\(\backslash\)'Ich \(\backslash\)u2665 B\(\backslash\)xFCcher\(\backslash\)',\(\backslash\)n\(\backslash\)t\(\backslash\)'foo \(\backslash\)uD834\(\backslash\)uDF06 bar\(\backslash\)'\(\backslash\)n]' }

\end{DoxyCode}


This setting has no effect on the output for strings.\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24141}{}\doxysubsubsection{$<$tt$>$indent$<$/tt$>$}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24141}
The {\ttfamily indent} option takes a string value, and defaults to `'\textbackslash{}t\textquotesingle{}{\ttfamily . When the}compact{\ttfamily setting is enabled (}true{\ttfamily ), the value of the}indent\`{} option is used to format the output for arrays and objects.


\begin{DoxyCode}{0}
\DoxyCodeLine{jsesc(\{ 'Ich ♥ Bücher': 'foo 𝌆 bar' \}, \{}
\DoxyCodeLine{  'compact': false,}
\DoxyCodeLine{  'indent': '\(\backslash\)t' // this is the default}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '\{\(\backslash\)n\(\backslash\)t\(\backslash\)'Ich \(\backslash\)u2665 B\(\backslash\)xFCcher\(\backslash\)': \(\backslash\)'foo \(\backslash\)uD834\(\backslash\)uDF06 bar\(\backslash\)'\(\backslash\)n\}'}
\DoxyCodeLine{}
\DoxyCodeLine{jsesc(\{ 'Ich ♥ Bücher': 'foo 𝌆 bar' \}, \{}
\DoxyCodeLine{  'compact': false,}
\DoxyCodeLine{  'indent': '  '}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '\{\(\backslash\)n  \(\backslash\)'Ich \(\backslash\)u2665 B\(\backslash\)xFCcher\(\backslash\)': \(\backslash\)'foo \(\backslash\)uD834\(\backslash\)uDF06 bar\(\backslash\)'\(\backslash\)n\}'}
\DoxyCodeLine{}
\DoxyCodeLine{jsesc([ 'Ich ♥ Bücher', 'foo 𝌆 bar' ], \{}
\DoxyCodeLine{  'compact': false,}
\DoxyCodeLine{  'indent': '  '}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '[\(\backslash\)n  \(\backslash\)'Ich \(\backslash\)u2665 B\(\backslash\)xFCcher\(\backslash\)',\(\backslash\)n\(\backslash\)  t\(\backslash\)'foo \(\backslash\)uD834\(\backslash\)uDF06 bar\(\backslash\)'\(\backslash\)n]' }

\end{DoxyCode}


This setting has no effect on the output for strings.\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24142}{}\doxysubsubsection{$<$tt$>$json$<$/tt$>$}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24142}
The {\ttfamily json} option takes a boolean value ({\ttfamily true} or {\ttfamily false}), and defaults to {\ttfamily false} (disabled). When enabled, the output is valid JSON. \href{http://mathiasbynens.be/notes/javascript-escapes\#hexadecimal}{\texttt{ Hexadecimal character escape sequences}} and \href{http://mathiasbynens.be/notes/javascript-escapes\#single}{\texttt{ the {\ttfamily \textbackslash{}v} or {\ttfamily \textbackslash{}0} escape sequences}} will not be used. Setting {\ttfamily json\+: true} implies `quotes\+: \textquotesingle{}double', wrap\+: true, es6\+: false\`{}, although these values can still be overridden if needed — but in such cases, the output won’t be valid JSON anymore.


\begin{DoxyCode}{0}
\DoxyCodeLine{jsesc('foo\(\backslash\)x00bar\(\backslash\)xFF\(\backslash\)uFFFDbaz', \{}
\DoxyCodeLine{  'json': true}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '"{}foo\(\backslash\)\(\backslash\)u0000bar\(\backslash\)\(\backslash\)u00FF\(\backslash\)\(\backslash\)uFFFDbaz"{}'}
\DoxyCodeLine{}
\DoxyCodeLine{jsesc(\{ 'foo\(\backslash\)x00bar\(\backslash\)xFF\(\backslash\)uFFFDbaz': 'foo\(\backslash\)x00bar\(\backslash\)xFF\(\backslash\)uFFFDbaz' \}, \{}
\DoxyCodeLine{  'json': true}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '\{"{}foo\(\backslash\)\(\backslash\)u0000bar\(\backslash\)\(\backslash\)u00FF\(\backslash\)\(\backslash\)uFFFDbaz"{}:"{}foo\(\backslash\)\(\backslash\)u0000bar\(\backslash\)\(\backslash\)u00FF\(\backslash\)\(\backslash\)uFFFDbaz"{}\}'}
\DoxyCodeLine{}
\DoxyCodeLine{jsesc([ 'foo\(\backslash\)x00bar\(\backslash\)xFF\(\backslash\)uFFFDbaz', 'foo\(\backslash\)x00bar\(\backslash\)xFF\(\backslash\)uFFFDbaz' ], \{}
\DoxyCodeLine{  'json': true}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '["{}foo\(\backslash\)\(\backslash\)u0000bar\(\backslash\)\(\backslash\)u00FF\(\backslash\)\(\backslash\)uFFFDbaz"{},"{}foo\(\backslash\)\(\backslash\)u0000bar\(\backslash\)\(\backslash\)u00FF\(\backslash\)\(\backslash\)uFFFDbaz"{}]'}
\DoxyCodeLine{}
\DoxyCodeLine{// Values that are acceptable in JSON but aren’t strings, arrays, or object}
\DoxyCodeLine{// literals can’t be escaped, so they’ll just be preserved:}
\DoxyCodeLine{jsesc([ 'foo\(\backslash\)x00bar', [1, '©', \{ 'foo': true, 'qux': null \}], 42 ], \{}
\DoxyCodeLine{  'json': true}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '["{}foo\(\backslash\)\(\backslash\)u0000bar"{},[1,"{}\(\backslash\)\(\backslash\)u00A9"{},\{"{}foo"{}:true,"{}qux"{}:null\}],42]'}
\DoxyCodeLine{// Values that aren’t allowed in JSON are run through `JSON.stringify()`:}
\DoxyCodeLine{jsesc([ undefined, -\/Infinity ], \{}
\DoxyCodeLine{  'json': true}
\DoxyCodeLine{\});}
\DoxyCodeLine{// → '[null,null]' }

\end{DoxyCode}


{\bfseries{Note\+:}} Using this option on objects or arrays that contain non-\/string values relies on {\ttfamily JSON.\+stringify()}. For legacy environments like IE ≤ 7, use \href{http://bestiejs.github.io/json3/}{\texttt{ a {\ttfamily JSON} polyfill}}.\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24143}{}\doxysubsection{$<$tt$>$jsesc.\+version$<$/tt$>$}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24143}
A string representing the semantic version number.\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24144}{}\doxysubsection{Using the $<$tt$>$jsesc$<$/tt$>$ binary}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24144}
To use the {\ttfamily jsesc} binary in your shell, simply install jsesc globally using npm\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{npm install -\/g jsesc }

\end{DoxyCode}


After that you will be able to escape strings from the command line\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ jsesc 'föo ♥ bår 𝌆 baz'}
\DoxyCodeLine{f\(\backslash\)xF6o \(\backslash\)u2665 b\(\backslash\)xE5r \(\backslash\)uD834\(\backslash\)uDF06 baz }

\end{DoxyCode}


To escape arrays or objects containing string values, use the {\ttfamily -\/o}/{\ttfamily -\/-\/object} option\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ jsesc -\/-\/object '\{ "{}föo"{}: "{}♥"{}, "{}bår"{}: "{}𝌆 baz"{} \}'}
\DoxyCodeLine{\{'f\(\backslash\)xF6o':'\(\backslash\)u2665','b\(\backslash\)xE5r':'\(\backslash\)uD834\(\backslash\)uDF06 baz'\} }

\end{DoxyCode}


To prettify the output in such cases, use the {\ttfamily -\/p}/{\ttfamily -\/-\/pretty} option\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ jsesc -\/-\/pretty '\{ "{}föo"{}: "{}♥"{}, "{}bår"{}: "{}𝌆 baz"{} \}'}
\DoxyCodeLine{\{}
\DoxyCodeLine{  'f\(\backslash\)xF6o': '\(\backslash\)u2665',}
\DoxyCodeLine{  'b\(\backslash\)xE5r': '\(\backslash\)uD834\(\backslash\)uDF06 baz'}
\DoxyCodeLine{\} }

\end{DoxyCode}


For valid JSON output, use the {\ttfamily -\/j}/{\ttfamily -\/-\/json} option\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ jsesc -\/-\/json -\/-\/pretty '\{ "{}föo"{}: "{}♥"{}, "{}bår"{}: "{}𝌆 baz"{} \}'}
\DoxyCodeLine{\{}
\DoxyCodeLine{  "{}f\(\backslash\)u00F6o"{}: "{}\(\backslash\)u2665"{},}
\DoxyCodeLine{  "{}b\(\backslash\)u00E5r"{}: "{}\(\backslash\)uD834\(\backslash\)uDF06 baz"{}}
\DoxyCodeLine{\} }

\end{DoxyCode}


Read a local JSON file, escape any non-\/\+ASCII symbols, and save the result to a new file\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ jsesc -\/-\/json -\/-\/object < data-\/raw.json > data-\/escaped.json }

\end{DoxyCode}


Or do the same with an online JSON file\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ curl -\/sL "{}http://git.io/aorKgQ"{} | jsesc -\/-\/json -\/-\/object > data-\/escaped.json }

\end{DoxyCode}


See {\ttfamily jsesc -\/-\/help} for the full list of options.\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24145}{}\doxysection{Support}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24145}
This library has been tested in at least Chrome 27-\/29, Firefox 3-\/22, Safari 4-\/6, Opera 10-\/12, IE 6-\/10, Node.\+js v0.\+10.\+0, Narwhal 0.\+3.\+2, Ringo\+JS 0.\+8-\/0.\+9, Phantom\+JS 1.\+9.\+0, and Rhino 1.\+7\+RC4.

{\bfseries{Note\+:}} Using the {\ttfamily json} option on objects or arrays that contain non-\/string values relies on {\ttfamily JSON.\+parse()}. For legacy environments like IE ≤ 7, use \href{http://bestiejs.github.io/json3/}{\texttt{ a {\ttfamily JSON} polyfill}}.\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24146}{}\doxysection{Unit tests \& code coverage}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24146}
After cloning this repository, run {\ttfamily npm install} to install the dependencies needed for development and testing. You may want to install Istanbul {\itshape globally} using {\ttfamily npm install istanbul -\/g}.

Once that’s done, you can run the unit tests in Node using {\ttfamily npm test} or {\ttfamily node tests/tests.\+js}. To run the tests in Rhino, Ringo, Narwhal, and web browsers as well, use {\ttfamily grunt test}.

To generate the code coverage report, use {\ttfamily grunt cover}.\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24147}{}\doxysection{Author}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24147}
\tabulinesep=1mm
\begin{longtabu}spread 0pt [c]{*{1}{|X[-1]}|}
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ \href{https://twitter.com/mathias}{\texttt{ }}    }\\\cline{1-1}
\endfirsthead
\hline
\endfoot
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ \href{https://twitter.com/mathias}{\texttt{ }}    }\\\cline{1-1}
\endhead
\href{http://mathiasbynens.be/}{\texttt{ Mathias Bynens}}   \\\cline{1-1}
\end{longtabu}
\hypertarget{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24148}{}\doxysection{License}\label{md_sfcode_node_modules_regjsparser_node_modules_jsesc_README_autotoc_md24148}
This library is available under the \href{http://mths.be/mit}{\texttt{ MIT}} license. 