Quick Take
import { strict as assert } from "assert";
import { rRegex } from "ranges-regex";
const oldString = `The quick brown fox jumps over the lazy dog.`;
const result = rRegex(/the/gi, oldString);
// all regex matches, but in Ranges notation (see codsen.com/ranges/):
assert.deepEqual(result, [
[0, 3],
[31, 34],
]);
// if you slice the ranges, you'll get original regex caught values:
assert.deepEqual(
result.map(([from, to]) =>
oldString.slice(from, to)
),
["The", "the"]
);
Examples
- program example: Clashing replacement values
- program example: Nothing was found
- program example: Replacing values
Purpose
Takes a string, matches the given regex on it and returns ranges which would do the same thing.
Similarly to String.prototype.match()
, a no-results case will yield null
(which ranges-merge and others would gladly accept).
API
rRegex( regexp, str, [replacement] )
In other words, it's a function which takes three input arguments, third one is optional (marked with square brackets).
Input argument | Type | Obligatory? | Description |
---|---|---|---|
regexp |
Regular expression | yes | Provide the regexp to apply onto a string |
str |
String | yes | Provide a string upon which to match the regex |
replacement |
String or null |
no | If you want to add a third argument on every of the finding's third argument values, put it here. |
Output: array of zero or more arrays (so-called ranges) where each consists of two or more natural number (or zero) indexes OR null
.
You can use all the features of regexes: global, case insensitive flags and so on.
This package does not mutate its inputs.
If the input arguments' types are incorrect or absent, library will throw
an error.
Changelog
See it in the monorepo , on GitHub.
Contributing
To report bugs or request features or assistance, raise an issue on GitHub .
Any code contributions welcome! All Pull Requests will be dealt promptly.
Licence
Copyright © 2010–2021 Roy Revelt and other contributors