split-string
Split a string on a character except when the character is escaped.
Install
Install with npm:
$ npm install --save split-string
Why use this?
Although it's easy to split on a string:
console.log('a.b.c'.split('.'));
It's more challenging to split a string whilst respecting escaped or quoted characters.
Bad
console.log('a\\.b.c'.split('.'));
console.log('"a.b.c".d'.split('.'));
Good
var split = require('split-string');
console.log(split('a\\.b.c'));
console.log(split('"a.b.c".d'));
See the options to learn how to choose the separator or retain quotes or escaping.
Usage
var split = require('split-string');
split('a.b.c');
split('a.b.c\\.d');
split('a."b.c.d".e');
Brackets
Also respects brackets ({}
, `[]
split('a (b c d) e', ' ');
Options
options.brackets
Type: object|boolean
Default: undefined
Description
If enabled, split-string will not split inside brackets. The following brackets types are supported when options.brackets
is true
,
{
'<': '>',
'(': ')',
'[': ']',
'{': '}'
}
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.
Examples
split('a.{b.c}');
split('a.{b.c}', {brackets: true});
split('[a.b].(c.d)', {brackets: {'[', ']'}});
options.sep
Type: string
Default: .
The separator/character to split on.
Example
split('a.b,c', {sep: ','});
split('a.b,c', ',');
options.keepEscaping
Type: boolean
Default: undefined
Keep backslashes in the result.
Example
split('a.b\\.c');
split('a.b.\\c', {keepEscaping: true});
options.keepQuotes
Type: boolean
Default: undefined
Keep single- or double-quotes in the result.
Example
split('a."b.c.d".e');
split('a."b.c.d".e', {keepQuotes: true});
split('a.\'b.c.d\'.e', {keepQuotes: true});
options.keepDoubleQuotes
Type: boolean
Default: undefined
Keep double-quotes in the result.
Example
split('a."b.c.d".e');
split('a."b.c.d".e', {keepDoubleQuotes: true});
options.keepSingleQuotes
Type: boolean
Default: undefined
Keep single-quotes in the result.
Example
split('a.\'b.c.d\'.e');
split('a.\'b.c.d\'.e', {keepSingleQuotes: true});
Customizer
Type: function
Default: undefined
Pass a function as the last argument to customize how tokens are added to the array.
Example
var arr = split('a.b', function(tok) {
if (tok.arr[tok.arr.length - 1] === 'a') {
tok.split = false;
}
});
console.log(arr);
Properties
The tok
object has the following properties:
tok.val
(string) The current value about to be pushed onto the result arraytok.idx
(number) the current index in the stringtok.str
(string) the entire stringtok.arr
(array) the result array
Release history
v3.0.0 - 2017-06-17
Added
- adds support for brackets
About
Related projects
- deromanize: Convert roman numerals to arabic numbers (useful for books, outlines, documentation, slide decks, etc) | homepage
- randomatic: Generate randomized strings of a specified length, fast. Only the length is necessary, but you… more | homepage
- repeat-string: Repeat the given string n times. Fastest implementation for repeating a string. | homepage
- romanize: Convert numbers to roman numerals (useful for books, outlines, documentation, slide decks, etc) | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Contributors
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb
Running tests
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:
$ npm install && npm test
Author
Jon Schlinkert
License
Copyright © 2017, Jon Schlinkert.
Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on June 15, 2017.