split('a (b c d) e',' ');//=> ['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
// no bracket support by defaultsplit('a.{b.c}');//=> [ 'a', '{b', 'c}' ]// support all basic bracket types: "<>{}[]()"split('a.{b.c}', {brackets:true});//=> [ 'a', '{b.c}' ]// also supports nested brackets split('a.{b.{c.d}.e}.f', {brackets:true});//=> [ 'a', '{b.{c.d}.e}', 'f' ]// support only the specified bracketssplit('[a.b].(c.d)', {brackets: {'[':']'}});//=> [ '[a.b]', '(c', 'd)' ]
options.sep
Type: string
Default: .
The separator/character to split on.
Example
split('a.b,c', {sep:','});//=> ['a.b', 'c']// you can also pass the separator as string as the last argumentsplit('a.b,c',',');//=> ['a.b', 'c']
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);//=> ['a.b']
Properties
The tok object has the following properties:
tok.val (string) The current value about to be pushed onto the result array
tok.idx (number) the current index in the string
tok.str (string) the entire string
tok.arr (array) the result array
Release history
v3.0.0 - 2017-06-17
Added
adds support for brackets
About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
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
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: