What is split-string?
The split-string npm package is used to split strings on a specified separator while respecting nested structures such as quotes, brackets, and parentheses. It provides a way to parse complex strings with multiple levels of nested delimiters.
What are split-string's main functionalities?
Basic string splitting
Splits a string into an array of substrings using the specified separator.
"a.b.c".split('.')
Splitting with escaped characters
Splits a string on a separator while allowing escaped characters to be included in the results.
splitString('a\.b.c', { separator: '.' })
Splitting with brackets
Splits a string on a separator while respecting nested brackets, keeping them as part of the split segments.
splitString('a.{b.c}.d', { brackets: true })
Customizing separators and brackets
Allows customization of separators and bracket pairs for more complex string splitting scenarios.
splitString('a.{b.c}.d', { separators: ['.', ' '], brackets: { '{': '}' } })
Other packages similar to split-string
string-split
This package offers similar functionality to split-string, allowing strings to be split into arrays of substrings. It does not, however, provide the same level of support for nested structures or escaped characters.
split
Split is another package that can turn a stream of text into a stream of lines. It's more focused on Node.js streams rather than simple string splitting and doesn't handle nested delimiters.
strsplit
Strsplit is a simple utility for splitting strings but lacks the advanced features of split-string, such as handling nested delimiters and escaped characters.
split-string
Split a string on a character except when the character is escaped.
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.
Install
Install with npm:
$ npm install --save split-string
Install with yarn:
$ yarn add split-string
Usage
var split = require('split-string');
split('a.b.c');
split('a.b.c\\.d');
split('a."b.c.d".e');
Options
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
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 arabic 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 April 27, 2017.