Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
split-string
Advanced tools
Easy way to split a string on a given character unless it's quoted or escaped.
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.
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: { '{': '}' } })
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 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 is a simple utility for splitting strings but lacks the advanced features of split-string, such as handling nested delimiters and escaped characters.
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 :heart: and support.
Install with npm:
$ npm install --save split-string
const split = require('split-string');
console.log(split('a.b.c'));
//=> ['a', 'b', 'c']
// respects escaped characters
console.log(split('a.b.c\\.d'));
//=> ['a', 'b', 'c.d']
// respects double-quoted strings
console.log(split('a."b.c.d".e'));
//=> ['a', '"b.c.d"', 'e']
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 behavior
console.log(split('a.b."c.d.e.f.g".h.i'));
//=> [ 'a', 'b', '"c', 'd', 'e', 'f', 'g"', 'h', 'i' ]
// with quotes
console.log(split('a.b."c.d.e.f.g".h.i', { quotes: ['"'] }));
//=> [ 'a', 'b', '"c.d.e.f.g"', 'h', 'i' ]
// escaped quotes will be ignored
console.log(split('a.b.\\"c.d."e.f.g".h.i', { quotes: ['"'] }));
//=> [ 'a', 'b', '"c', 'd', '"e.f.g"', 'h', 'i' ]
// example of how to exclude non-escaped quotes from the result
let keep = (value, state) => {
return value !== '\\' && (value !== '"' || state.prev() === '\\');
};
console.log(split('a.b.\\"c.d."e.f.g".h.i', { quotes: ['"'], keep }));
//=> [ 'a', 'b', '"c', 'd', 'e.f.g', 'h', 'i' ]
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 behavior
console.log(split('a.{b.c}.{d.e}'));
//=> [ 'a', '{b', 'c}', '{d', 'e}' ]
When options.brackets
is true
, the following brackets types are supported:
{
'<': '>',
'(': ')',
'[': ']',
'{': '}'
}
For example:
console.log(split('a.{b.c}.{d.e}', { brackets: true }));
//=> [ '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 default
console.log(split('a.{b.c}.[d.e].f'));
//=> [ 'a', '{b', 'c}', '[d', 'e]', 'f' ]
// tell split-string not to split inside curly braces
console.log(split('a.{b.c}.[d.e].f', { brackets: { '{': '}' }}));
//=> [ 'a', '{b.c}', '[d', 'e]', 'f' ]
// tell split-string not to split inside any of these types: "<>{}[]()"
console.log(split('a.{b.c}.[d.e].f', { brackets: true }));
//=> [ 'a', '{b.c}', '[d.e]', 'f' ]
// ...nested brackets are also supported
console.log(split('a.{b.{c.d}.e}.f', { brackets: true }));
//=> [ 'a', '{b.{c.d}.e}', 'f' ]
// tell split-string not to split inside the given custom types
console.log(split('«a.b».⟨c.d⟩.[e.f]', { brackets: { '«': '»', '⟨': '⟩' } }));
//=> [ '«a.b»', '⟨c.d⟩', '[e', 'f]' ]
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.log(split('a.b\\.c')); //=> ['a', 'b.c']
// keep all characters
console.log(split('a.b.\\c', { keep: () => true })); //=> ['a', 'b\.c']
Type: string
Default: .
The character to split on.
Example
console.log(split('a.b,c', { separator: ',' })); //=> ['a.b', 'c']
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.log(split('a.b.c.a.d.e', state => state.prev() === 'a'));
//=> [ '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 nodesPull requests and stars are always welcome. For bugs and feature requests, please create an issue.
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
(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
You might also be interested in these projects:
Commits | Contributor |
---|---|
56 | jonschlinkert |
12 | doowb |
6 | Ovyerus |
1 | silverwind |
Jon Schlinkert
Copyright © 2019, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on April 22, 2019.
FAQs
Easy way to split a string on a given character unless it's quoted or escaped.
The npm package split-string receives a total of 6,410,583 weekly downloads. As such, split-string popularity was classified as popular.
We found that split-string demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.