split-string
Easy way to split a string on a given character unless it's quoted or escaped.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.
Install
Install with npm:
$ npm install --save split-string
Usage
const split = ; console;//=> ['a', 'b', 'c'] // respects escaped charactersconsole;//=> ['a', 'b', 'c.d'] // respects double-quoted stringsconsole;//=> ['a', '"b.c.d"', 'e']
Options
options.quotes
Type: Array|Boolean
Default: []
Description
Tell split-string not to split inside any of the quote characters specified on the quotes option. Each character signifies both the "opening" and "closing" character to use.
// default behaviorconsole;//=> [ 'a', 'b', '"c', 'd', 'e', 'f', 'g"', 'h', 'i' ] // with quotesconsole;//=> [ 'a', 'b', '"c.d.e.f.g"', 'h', 'i' ] // escaped quotes will be ignoredconsole;//=> [ 'a', 'b', '"c', 'd', '"e.f.g"', 'h', 'i' ] // example of how to exclude non-escaped quotes from the resultlet { return value !== '\\' && value !== '"' || state === '\\';};console;//=> [ 'a', 'b', '"c', 'd', 'e.f.g', 'h', 'i' ]
Options
options.brackets
Type: Object|Boolean
Default: {}
Description
By default, no special significance is given to bracket-like characters (such as square brackets, curly braces, angle brackets, and so on).
// default behaviorconsole;//=> [ 'a', '{b', 'c}', '{d', 'e}' ]
When options.brackets
is true
, the following brackets types are supported:
'<': '>' '(': ')' '[': ']' '{': '}'
For example:
console;//=> [ 'a', '{b.c}', '{d.e}' ]
Alternatively, an object of brackets may be passed, where each key is the opening bracket and each value is the corresponding closing bracket. Note that the key and value must be different characters. If you want to use the same character for both open and close, use the quotes option.
Examples
// no bracket support by defaultconsole;//=> [ 'a', '{b', 'c}', '[d', 'e]', 'f' ] // tell split-string not to split inside curly bracesconsole;//=> [ 'a', '{b.c}', '[d', 'e]', 'f' ] // tell split-string not to split inside any of these types: "<>{}[]()"console;//=> [ 'a', '{b.c}', '[d.e]', 'f' ] // ...nested brackets are also supportedconsole;//=> [ 'a', '{b.{c.d}.e}', 'f' ] // tell split-string not to split inside the given custom typesconsole;//=> [ '«a.b»', '⟨c.d⟩', '[e', 'f]' ]
options.keep
Type: function
Default: Function that returns true if the character is not \\
.
Function that returns true when a character should be retained in the result.
Example
console; //=> ['a', 'b.c'] // keep all charactersconsole; //=> ['a', 'b\.c']
options.separator
Type: string
Default: .
The character to split on.
Example
console; //=> ['a.b', 'c']
Split function
Optionally pass a function as the last argument to tell split-string whether or not to split when the specified separator is encountered.
Example
// only split on "." when the "previous" character is "a"console;//=> [ 'a', 'b.c.a', 'd.e' ]
The state
object exposes the following properties:
input
- (String) The un-modified, user-defined input stringseparator
- (String) the specified separator to split on.index
- (Number) The current cursor positionvalue
- (String) The character at the current indexbos
- (Function) Returns true if position is at the beginning-of-stringeos
- (Function) Returns true if position is at the end-of-stringprev
- (Function) Returns the previously scanned characternext
- (Function) Returns the next character after the current positionblock
- (Object) The "current" AST node.stack
- (Array) AST nodes
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:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
Related projects
You might also be interested in these 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 using simple character sequences. The original generate-password. | 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
Contributors
Commits | Contributor |
---|---|
56 | jonschlinkert |
12 | doowb |
6 | Ovyerus |
1 | silverwind |
Author
Jon Schlinkert
License
Copyright © 2019, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on April 22, 2019.