githubEdit

split-string

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

Please consider following this project's author, Jon Schlinkertarrow-up-right, and consider starring the project to show your ❤️ and support.

Install

Install with npmarrow-up-right:

$ npm install --save split-string
chevron-rightWhy use this?hashtag

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.

Bad

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

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

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:

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

options.sep

Type: string

Default: .

The separator/character to split on.

Example

options.keepEscaping

Type: boolean

Default: undefined

Keep backslashes in the result.

Example

options.keepQuotes

Type: boolean

Default: undefined

Keep single- or double-quotes in the result.

Example

options.keepDoubleQuotes

Type: boolean

Default: undefined

Keep double-quotes in the result.

Example

options.keepSingleQuotes

Type: boolean

Default: undefined

Keep single-quotes in the result.

Example

Customizer

Type: function

Default: undefined

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

Example

Properties

The tok object has the following properties:

  • tok.val (string) The current value about to be pushed onto the result array

  • tok.idx (number) the current index in the string

  • tok.str (string) the entire string

  • tok.arr (array) the result array

Release history

v3.0.0 - 2017-06-17

Added

  • adds support for brackets

About

chevron-rightContributinghashtag

Pull requests and stars are always welcome. For bugs and feature requests, please create an issuearrow-up-right.

chevron-rightRunning Testshashtag

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:

chevron-rightBuilding docshashtag

(This project's readme.md is generated by verbarrow-up-right, please don't edit the readme directly. Any changes to the readme must be made in the .verb.mdarrow-up-right readme template.)

To generate the readme, run the following command:

You might also be interested in these projects:

Contributors

Author

Jon Schlinkert

License

Copyright © 2017, Jon Schlinkertarrow-up-right. Released under the MIT Licensearrow-up-right.


This file was generated by verb-generate-readmearrow-up-right, v0.6.0, on November 19, 2017.

Last updated

Was this helpful?