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.
Install
Install with npm:
$ npm install --save 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.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});
options.strict
Type: Boolean
Default: undefined
When true
or undefined
, throws an error on unclosed double and single quotes.
Set to false
to ignore errors and continue parsing.
Example
split('a.\'b.c', {strict: false});
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.4.3, on April 11, 2017.