1 # split-string [![NPM version](https://img.shields.io/npm/v/split-string.svg?style=flat)](https://www.npmjs.com/package/split-string) [![NPM monthly downloads](https://img.shields.io/npm/dm/split-string.svg?style=flat)](https://npmjs.org/package/split-string) [![NPM total downloads](https://img.shields.io/npm/dt/split-string.svg?style=flat)](https://npmjs.org/package/split-string) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/split-string.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/split-string)
3 > Split a string on a character except when the character is escaped.
5 Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
9 Install with [npm](https://www.npmjs.com/):
12 $ npm install --save split-string
15 <!-- section: Why use this? -->
18 <summary><strong>Why use this?</strong></summary>
22 Although it's easy to split on a string:
25 console.log('a.b.c'.split('.'));
29 It's more challenging to split a string whilst respecting escaped or quoted characters.
34 console.log('a\\.b.c'.split('.'));
35 //=> ['a\\', 'b', 'c']
37 console.log('"a.b.c".d'.split('.'));
38 //=> ['"a', 'b', 'c"', 'd']
44 var split = require('split-string');
45 console.log(split('a\\.b.c'));
48 console.log(split('"a.b.c".d'));
52 See the [options](#options) to learn how to choose the separator or retain quotes or escaping.
61 var split = require('split-string');
66 // respects escaped characters
68 //=> ['a', 'b', 'c.d']
70 // respects double-quoted strings
72 //=> ['a', 'b.c.d', 'e']
77 Also respects brackets [unless disabled](#optionsbrackets):
80 split('a (b c d) e', ' ');
81 //=> ['a', '(b c d)', 'e']
88 **Type**: `object|boolean`
90 **Default**: `undefined`
94 If enabled, split-string will not split inside brackets. The following brackets types are supported when `options.brackets` is `true`,
105 Or, if object of brackets must be passed, each property on the object must be a bracket type, where the property key is the opening delimiter and property value is the closing delimiter.
110 // no bracket support by default
112 //=> [ 'a', '{b', 'c}' ]
114 // support all basic bracket types: "<>{}[]()"
115 split('a.{b.c}', {brackets: true});
116 //=> [ 'a', '{b.c}' ]
118 // also supports nested brackets
119 split('a.{b.{c.d}.e}.f', {brackets: true});
120 //=> [ 'a', '{b.{c.d}.e}', 'f' ]
122 // support only the specified brackets
123 split('[a.b].(c.d)', {brackets: {'[': ']'}});
124 //=> [ '[a.b]', '(c', 'd)' ]
133 The separator/character to split on.
138 split('a.b,c', {sep: ','});
141 // you can also pass the separator as string as the last argument
146 ### options.keepEscaping
150 **Default**: `undefined`
152 Keep backslashes in the result.
160 split('a.b.\\c', {keepEscaping: true});
164 ### options.keepQuotes
168 **Default**: `undefined`
170 Keep single- or double-quotes in the result.
175 split('a."b.c.d".e');
176 //=> ['a', 'b.c.d', 'e']
178 split('a."b.c.d".e', {keepQuotes: true});
179 //=> ['a', '"b.c.d"', 'e']
181 split('a.\'b.c.d\'.e', {keepQuotes: true});
182 //=> ['a', '\'b.c.d\'', 'e']
185 ### options.keepDoubleQuotes
189 **Default**: `undefined`
191 Keep double-quotes in the result.
196 split('a."b.c.d".e');
197 //=> ['a', 'b.c.d', 'e']
199 split('a."b.c.d".e', {keepDoubleQuotes: true});
200 //=> ['a', '"b.c.d"', 'e']
203 ### options.keepSingleQuotes
207 **Default**: `undefined`
209 Keep single-quotes in the result.
214 split('a.\'b.c.d\'.e');
215 //=> ['a', 'b.c.d', 'e']
217 split('a.\'b.c.d\'.e', {keepSingleQuotes: true});
218 //=> ['a', '\'b.c.d\'', 'e']
225 **Default**: `undefined`
227 Pass a function as the last argument to customize how tokens are added to the array.
232 var arr = split('a.b', function(tok) {
233 if (tok.arr[tok.arr.length - 1] === 'a') {
243 The `tok` object has the following properties:
245 * `tok.val` (string) The current value about to be pushed onto the result array
246 * `tok.idx` (number) the current index in the string
247 * `tok.str` (string) the entire string
248 * `tok.arr` (array) the result array
252 ### v3.0.0 - 2017-06-17
256 * adds support for brackets
261 <summary><strong>Contributing</strong></summary>
263 Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
268 <summary><strong>Running Tests</strong></summary>
270 Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
273 $ npm install && npm test
279 <summary><strong>Building docs</strong></summary>
281 _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
283 To generate the readme, run the following command:
286 $ npm install -g verbose/verb#dev verb-generate-readme && verb
293 You might also be interested in these projects:
295 * [deromanize](https://www.npmjs.com/package/deromanize): Convert roman numerals to arabic numbers (useful for books, outlines, documentation, slide decks, etc) | [homepage](https://github.com/jonschlinkert/deromanize "Convert roman numerals to arabic numbers (useful for books, outlines, documentation, slide decks, etc)")
296 * [randomatic](https://www.npmjs.com/package/randomatic): Generate randomized strings of a specified length using simple character sequences. The original generate-password. | [homepage](https://github.com/jonschlinkert/randomatic "Generate randomized strings of a specified length using simple character sequences. The original generate-password.")
297 * [repeat-string](https://www.npmjs.com/package/repeat-string): Repeat the given string n times. Fastest implementation for repeating a string. | [homepage](https://github.com/jonschlinkert/repeat-string "Repeat the given string n times. Fastest implementation for repeating a string.")
298 * [romanize](https://www.npmjs.com/package/romanize): Convert numbers to roman numerals (useful for books, outlines, documentation, slide decks, etc) | [homepage](https://github.com/jonschlinkert/romanize "Convert numbers to roman numerals (useful for books, outlines, documentation, slide decks, etc)")
302 | **Commits** | **Contributor** |
304 | 28 | [jonschlinkert](https://github.com/jonschlinkert) |
305 | 9 | [doowb](https://github.com/doowb) |
311 * [github/jonschlinkert](https://github.com/jonschlinkert)
312 * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
316 Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
317 Released under the [MIT License](LICENSE).
321 _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on November 19, 2017._