Socket
Socket
Sign inDemoInstall

split-string

Package Overview
Dependencies
8
Maintainers
2
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    split-string

Split a string on a character except when the character is escaped.


Version published
Weekly downloads
10M
decreased by-14.91%
Maintainers
2
Install size
149 kB
Created
Weekly downloads
 

Package description

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

Readme

Source

split-string NPM version NPM monthly downloads NPM total downloads Linux Build Status

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

Install with npm:

$ npm install --save split-string
Why use this?

Although it's easy to split on a string:

console.log('a.b.c'.split('.'));
//=> ['a', 'b', 'c']

It's more challenging to split a string whilst respecting escaped or quoted characters.

This is bad

console.log('a\\.b.c'.split('.'));
//=> ['a\\', 'b', 'c']

console.log('"a.b.c".d'.split('.'));
//=> ['"a', 'b', 'c"', 'd']

This is good

var split = require('split-string');
console.log(split('a\\.b.c'));
//=> ['a.b', 'c']

console.log(split('"a.b.c".d'));
//=> ['a.b.c', 'd']

See the options to learn how to choose the separator or retain quotes or escaping.


Usage

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']

Options

options.brackets

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 bracket types
split('«a.b».⟨c.d⟩', {brackets: {'«': '»', '⟨': '⟩'}});
//=> [ '«a.b»', '⟨c.d⟩' ]
split('a.{a.[{b.c}].d}.e', {brackets: {'[': ']'}});
//=> [ 'a', '{a', '[{b.c}]', 'd}', 'e' ]

options.keepEscaping

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']

options.keepQuotes

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']

options.keepDoubleQuotes

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']

options.keepSingleQuotes

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']

options.separator

Type: string

Default: .

The separator/character to split on. Aliased as options.sep for backwards compatibility with versions <4.0.

Example

split('a.b,c', {separator: ','});
//=> ['a.b', 'c']

// you can also pass the separator as a string as the last argument
split('a.b,c', ',');
//=> ['a.b', 'c']

options.split

Type: function

Default: the default function returns true

Pass a custom function to be called each time a separator is encountered. If the function returns false the string will not be split on that separator.

Example

const arr = split('a.b.c', {
  split: function() {
    const prev = this.prev();
    if (prev && prev.value === 'a') {
      return false;
    }
    return true;
  }
});
console.log(arr);
//=> ['a.b', 'c']

Note that the snapdragon-lexer instance is exposed as this inside the function. See snapdragon-lexer for more information and complete API documentation.

Customizer

Type: function

Default: undefined

Pass a function as the last argument to customize how tokens are added to the array.

Example

var res = split('a.b', function(token) {
  if (token.tokens[0] === 'a') {
    token.split = false;
  }
});
console.log(res);
//=> ['a.b']

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

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

CommitsContributor
36jonschlinkert
10doowb

Author

Jon Schlinkert

License

Copyright © 2018, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on January 08, 2018.

Keywords

FAQs

Last updated on 08 Jan 2018

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc