splitargs2
v0.1.3
Published
Splits a string into tokens by a given separator, treating any quoted parts as a single token.
Downloads
9,392
Maintainers
Readme
splitargs2
Splits a string into tokens by a given separator, treating any quoted parts as a single token.
This project is a fork of splitargs which has been completely rewritten to use modern JavaScript standards.
Installation
- With npm:
npm i -s splitargs2
- With yarn:
yarn add splitargs2
Usage
splitargs(text, ?keepQuotes, ?separator)
Parameters:
- text
string
The string to tokenize optional
keepQuotesboolean
If true, quoted tokens will maintain their quote characters. Default:false
optional
separator?RegExp
Matches separators for tokens. Default:/\s/g
Returns: string[]
The tokens extracted from the text.
const splitargs = require('splitargs2');
console.log(splitargs("Hello, world!"));
// [ 'Hello,', 'world!' ]
console.log(splitargs("My name is \"Stan Pines\""));
// [ 'My', 'name', 'is', 'Stan Pines' ]
// With keepQuotes as true:
console.log(splitargs('Keep "these quotes," please.', true));
// [ 'Keep', '"these quotes,"', 'please.' ]
// Using a custom separator:
const dashMatcher = /\-/g;
console.log(splitargs('4000-1234-5678-9010', false, dashMatcher));
// [ '4000', '1234', '5678', '9010' ]
License
ISC License
Copyright 2018 Shinobu1337
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.