# jsesc [![Build status](https://travis-ci.org/mathiasbynens/jsesc.svg?branch=master)](https://travis-ci.org/mathiasbynens/jsesc) [![Code coverage status](http://img.shields.io/coveralls/mathiasbynens/jsesc/master.svg)](https://coveralls.io/r/mathiasbynens/jsesc) [![Dependency status](https://gemnasium.com/mathiasbynens/jsesc.svg)](https://gemnasium.com/mathiasbynens/jsesc)
This is a JavaScript library for [escaping JavaScript strings](http://mathiasbynens.be/notes/javascript-escapes) while generating the shortest possible valid ASCII-only output. [Hereâs an online demo.](http://mothereff.in/js-escapes)
This can be used to avoid [mojibake](http://en.wikipedia.org/wiki/Mojibake) and other encoding issues, or even to [avoid errors](https://twitter.com/annevk/status/380000829643571200) when passing JSON-formatted data (which may contain U+2028 LINE SEPARATOR, U+2029 PARAGRAPH SEPARATOR, or [lone surrogates](http://esdiscuss.org/topic/code-points-vs-unicode-scalar-values#content-14)) to a JavaScript parser or an UTF-8 encoder, respectively.
Feel free to fork if you see possible improvements!
## Installation
Via [Bower](http://bower.io/):
```bash
bower install jsesc
```
Via [Component](https://github.com/component/component):
```bash
component install mathiasbynens/jsesc
```
Via [npm](http://npmjs.org/):
```bash
npm install jsesc
```
In a browser:
```html
<script src="jsesc.js"></script>
```
In [Node.js](http://nodejs.org/) and [RingoJS](http://ringojs.org/):
```js
var jsesc = require('jsesc');
```
In [Narwhal](http://narwhaljs.org/):
```js
var jsesc = require('jsesc').jsesc;
```
In [Rhino](http://www.mozilla.org/rhino/):
```js
load('jsesc.js');
```
Using an AMD loader like [RequireJS](http://requirejs.org/):
```js
require(
{
'paths': {
'jsesc': 'path/to/jsesc'
}
},
['jsesc'],
function(jsesc) {
console.log(jsesc);
}
);
```
## API
### `jsesc(value, options)`
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) [escape sequences for use in JavaScript strings](http://mathiasbynens.be/notes/javascript-escapes). The first supported value type is strings:
```js
jsesc('Ich ⥠Bücher');
// â 'Ich \\u2665 B\\xFCcher'
jsesc('foo ð bar');
// â 'foo \\uD834\\uDF06 bar'
```
Instead of a string, the `value` can also be an array, or an object. In such cases, `jsesc` will return a stringified version of the value where any characters that are not printable ASCII symbols are escaped in the same way.
```js
// Escaping an array
jsesc([
'Ich ⥠Bücher', 'foo ð bar'
]);
// â '[\'Ich \\u2665 B\\xFCcher\',\'foo \\uD834\\uDF06 bar\']'
// Escaping an object
jsesc({
'Ich ⥠Bücher': 'foo ð bar'
});
// â '{\'Ich \\u2665 B\\xFCcher\':\'foo \\uD834\\uDF06 bar\'}'
```
The optional `options` argument accepts an object with the following options:
#### `quotes`
The default value for the `quotes` option is `'single'`. This means that any occurences of `'` in the input string will be escaped as `\'`, so that the output can be used in a string literal wrapped in single quotes.
```js
jsesc('Lorem ipsum "dolor" sit \'amet\' etc.');
// â 'Lorem ipsum "dolor" sit \\\'amet\\\' etc.'
jsesc('Lorem ipsum "dolor" sit \'amet\' etc.', {
'quotes': 'single'
});
// â 'Lorem ipsum "dolor" sit \\\'amet\\\' etc.'
// â "Lorem ipsum \"dolor\" sit \\'amet\\' etc."
```
If you want to use the output as part of a string literal wrapped in double quotes, set the `quotes` option to `'double'`.
```js
jsesc('Lorem ipsum "dolor" sit \'amet\' etc.', {
'quotes': 'double'
});
// â 'Lorem ipsum \\"dolor\\" sit \'amet\' etc.'
// â "Lorem ipsum \\\"dolor\\\" sit 'amet' etc."
```
This setting also affects the output for arrays and objects:
```js
jsesc({ 'Ich ⥠Bücher': 'foo ð bar' }, {
'quotes': 'double'
});
// â '{"Ich \\u2665 B\\xFCcher":"foo \\uD834\\uDF06 bar"}'
jsesc([ 'Ich ⥠Bücher', 'foo ð bar' ], {
'quotes': 'double'
});
// â '["Ich \\u2665 B\\xFCcher","foo \\uD834\\uDF06 bar"]'
```
#### `wrap`
The `wrap` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, the output will be a valid JavaScript string literal wrapped in quotes. The type of quotes can be specified through the `quotes` setting.
```js
jsesc('Lorem ipsum "dolor" sit \'amet\' etc.', {
'quotes': 'single',
'wrap': true
});
// â '\'Lorem ipsum "dolor" sit \\\'amet\\\' etc.\''
// â "\'Lorem ipsum \"dolor\" sit \\\'amet\\\' etc.\'"
jsesc('Lorem ipsum "dolor" sit \'amet\' etc.', {
'quotes': 'double',
'wrap': true
});
// â '"Lorem ipsum \\"dolor\\" sit \'amet\' etc."'
// â "\"Lorem ipsum \\\"dolor\\\" sit \'amet\' etc.\""
```
#### `es6`
The `es6` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, any astral Unicode symbols in the input will be escaped using [ECMAScript 6 Unicode code point escape sequences](http://mathiasbynens.be/notes/javascript-escapes#unicode-code-point) 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 `json` setting is enabled, the value for the `es6` setting is ignored (as if it was `false`).
```js
// By default, the `es6` option is disabled:
jsesc('foo ð bar ð© baz');
// â 'foo \\uD834\\uDF06 bar \\uD83D\\uDCA9 baz'
// To explicitly disable it:
jsesc('foo ð bar ð© baz', {
'es6': false
});
// â 'foo \\uD834\\uDF06 bar \\uD83D\\uDCA9 baz'
// To enable it:
jsesc('foo ð bar ð© baz', {
'es6': true
});
// â 'foo \\u{1D306} bar \\u{1F4A9} baz'
```
#### `escapeEverything`
The `escapeEverything` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, all the symbols in the output will be escaped, even printable ASCII symbols.
```js
jsesc('lolwat"foo\'bar', {
'escapeEverything': true
});
// â '\\x6C\\x6F\\x6C\\x77\\x61\\x74\\"\\x66\\x6F\\x6F\\\'\\x62\\x61\\x72'
// â "\\x6C\\x6F\\x6C\\x77\\x61\\x74\\\"\\x66\\x6F\\x6F\\'\\x62\\x61\\x72"
```
This setting also affects the output for arrays and objects:
```js
jsesc({ 'Ich ⥠Bücher': 'foo ð bar' }, {
'escapeEverything': true
});
// â '{\'\x49\x63\x68\x20\u2665\x20\x42\xFC\x63\x68\x65\x72\':\'\x66\x6F\x6F\x20\uD834\uDF06\x20\x62\x61\x72\'}'
// â "{'\x49\x63\x68\x20\u2665\x20\x42\xFC\x63\x68\x65\x72':'\x66\x6F\x6F\x20\uD834\uDF06\x20\x62\x61\x72'}"
jsesc([ 'Ich ⥠Bücher': 'foo ð bar' ], {
'escapeEverything': true
});
// â '[\'\x49\x63\x68\x20\u2665\x20\x42\xFC\x63\x68\x65\x72\',\'\x66\x6F\x6F\x20\uD834\uDF06\x20\x62\x61\x72\']'
```
#### `compact`
The `compact` option takes a boolean value (`true` or `false`), and defaults to `true` (enabled). When enabled, the output for arrays and objects will be as compact as possible; it wonât be formatted nicely.
```js
jsesc({ 'Ich ⥠Bücher': 'foo ð bar' }, {
'compact': true // this is the default
});
// â '{\'Ich \u2665 B\xFCcher\':\'foo \uD834\uDF06 bar\'}'
jsesc({ 'Ich ⥠Bücher': 'foo ð bar' }, {
'compact': false
});
// â '{\n\t\'Ich \u2665 B\xFCcher\': \'foo \uD834\uDF06 bar\'\n}'
jsesc([ 'Ich ⥠Bücher', 'foo ð bar' ], {
'compact': false
});
// â '[\n\t\'Ich \u2665 B\xFCcher\',\n\t\'foo \uD834\uDF06 bar\'\n]'
```
This setting has no effect on the output for strings.
#### `indent`
The `indent` option takes a string value, and defaults to `'\t'`. When the `compact` setting is enabled (`true`), the value of the `indent` option is used to format the output for arrays and objects.
```js
jsesc({ 'Ich ⥠Bücher': 'foo ð bar' }, {
'compact': false,
'indent': '\t' // this is the default
});
// â '{\n\t\'Ich \u2665 B\xFCcher\': \'foo \uD834\uDF06 bar\'\n}'
jsesc({ 'Ich ⥠Bücher': 'foo ð bar' }, {
'compact': false,
'indent':
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
小程序 商城功能,使用了vant weapp ui ,融合了微信登录,微信支付 (912个子文件)
jsesc.1 3KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.editorconfig 147B
.eslintignore 7B
.gitignore 28B
index.js 231KB
core.js 112KB
lodash.min.js 72KB
index.js 39KB
index.js 38KB
emit.js 33KB
emit.js 32KB
component.js 30KB
runtime.js 24KB
index.js 22KB
index.js 21KB
index.js 17KB
index.js 15KB
index.js 13KB
transition.js 13KB
index.js 13KB
core.min.js 12KB
index.js 12KB
index.js 11KB
index.js 11KB
app.js 11KB
index.js 10KB
index.js 10KB
home.js 10KB
app.js 10KB
index.js 10KB
toast.js 10KB
visit.js 9KB
cates.js 9KB
dialog.js 9KB
visit.js 9KB
util.js 9KB
base.js 8KB
index.js 8KB
index.js 8KB
index.js 8KB
writer.js 7KB
jsesc.js 7KB
page.js 7KB
wrapperLodash.js 7KB
safe-area.js 6KB
component.js 6KB
promise.js 6KB
index.js 6KB
debounce.js 6KB
index.js 6KB
behavior.js 6KB
index.js 6KB
reader.js 6KB
_baseClone.js 5KB
index.js 5KB
index.js 5KB
hoist.js 5KB
index.js 5KB
index.js 5KB
index.js 4KB
index.js 4KB
leap.js 4KB
hoist.js 4KB
index.js 4KB
index.js 4KB
index(1).js 4KB
index.js 4KB
index.js 4KB
index.js 4KB
index.js 4KB
index.js 4KB
touch.js 4KB
leap.js 4KB
baseAPI.js 4KB
index.js 4KB
_equalByTag.js 4KB
meta.js 4KB
_createWrap.js 4KB
index.js 4KB
index.js 4KB
notify.js 4KB
open-type.js 3KB
index.js 3KB
_deburrLetter.js 3KB
index.js 3KB
truncate.js 3KB
basic.js 3KB
_createHybrid.js 3KB
index.js 3KB
_mergeData.js 3KB
_baseMergeDeep.js 3KB
_unicodeWords.js 3KB
index.js 3KB
_baseIsEqualDeep.js 3KB
index.js 3KB
props.js 3KB
home.js 3KB
共 912 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
N201871643
- 粉丝: 1154
- 资源: 2608
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功