sxoinas12 82e781b8aa Graph & Tree | 5 years ago | |
---|---|---|
.. | ||
LICENSE | 5 years ago | |
README.md | 5 years ago | |
index.js | 5 years ago | |
package.json | 5 years ago |
Split a string on a character except when the character is escaped.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install with npm:
$ npm install --save split-string
var split = require('split-string');
split('a.b.c');
//=> ['a', 'b', 'c']
// respects escaped characters
split('a.b.c\\.d');
//=> ['a', 'b', 'c.d']
// respects double-quoted strings
split('a."b.c.d".e');
//=> ['a', 'b.c.d', 'e']
Brackets
Also respects brackets unless disabled:
split('a (b c d) e', ' ');
//=> ['a', '(b c d)', 'e']
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 default
split('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 brackets
split('[a.b].(c.d)', {brackets: {'[': ']'}});
//=> [ '[a.b]', '(c', 'd)' ]
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 argument
split('a.b,c', ',');
//=> ['a.b', 'c']
Type: boolean
Default: undefined
Keep backslashes in the result.
Example
split('a.b\\.c');
//=> ['a', 'b.c']
split('a.b.\\c', {keepEscaping: true});
//=> ['a', 'b\.c']
Type: boolean
Default: undefined
Keep single- or double-quotes in the result.
Example
split('a."b.c.d".e');
//=> ['a', 'b.c.d', 'e']
split('a."b.c.d".e', {keepQuotes: true});
//=> ['a', '"b.c.d"', 'e']
split('a.\'b.c.d\'.e', {keepQuotes: true});
//=> ['a', '\'b.c.d\'', 'e']
Type: boolean
Default: undefined
Keep double-quotes in the result.
Example
split('a."b.c.d".e');
//=> ['a', 'b.c.d', 'e']
split('a."b.c.d".e', {keepDoubleQuotes: true});
//=> ['a', '"b.c.d"', 'e']
Type: boolean
Default: undefined
Keep single-quotes in the result.
Example
split('a.\'b.c.d\'.e');
//=> ['a', 'b.c.d', 'e']
split('a.\'b.c.d\'.e', {keepSingleQuotes: true});
//=> ['a', '\'b.c.d\'', 'e']
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);
//=> ['a.b']
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 arrayAdded
You might also be interested in these projects:
| Commits | Contributor | | --- | --- | | 28 | jonschlinkert | | 9 | doowb |
Jon Schlinkert
Copyright © 2017, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on November 19, 2017.