Split string into array by chunks of whitespace
This package is pure ESM. If you're not ready yet, install an older version of this program, 2.1.0 (npm i string-split-by-whitespace@2.1.0
).
npm i string-split-by-whitespace
import { strict as assert } from "assert";
import { splitByW } from "string-split-by-whitespace";
// Split by whitespace is easy - use native String.prototype.split()
assert.deepEqual("abc def ghi".split(/\s+/), ["abc", "def", "ghi"]);
const source = "\n \n a\t \nb \n \t";
// this program is nearly equivalent to regex-based split:
assert.deepEqual(source.split(/\s+/), ["", "a", "b", ""]);
assert.deepEqual(splitByW(source), ["a", "b"]);
// regex-based split needs more filtration but it's native solution
// ADDITIONALLY...
// this program allows to exclude certain index ranges:
assert.deepEqual(
splitByW("a b c d e", {
ignoreRanges: [[0, 2]], // that's "a" and space after it
}),
["b", "c", "d", "e"],
);
Please visit codsen.com for a full description of the API.
To report bugs or request features or assistance, raise an issue on GitHub.
MIT License.
Copyright © 2010-2024 Roy Revelt and other contributors.