Registry / serialization / to-regex-range

to-regex-range

JSON →
library5.0.1jsnpmunverified

to-regex-range is a utility library that generates a regex-compatible source string for matching numeric ranges. It simplifies the complex task of creating regular expressions for ranges like '1 to 50' or '0001 to 5555', which can quickly become unwieldy to write manually. The current stable version is 5.0.1. While a specific release cadence isn't published, the library maintains a stable API. Key differentiators include its convenience in abstracting complex regex logic, extensive test validation (over 2.78 million assertions), and optimizations that produce smaller, efficient regular expressions by reducing duplicate sequences and utilizing fragment caching. It handles zero-padding and negative numbers, making it robust for various validation or parsing scenarios.

npm install to-regex-range
INSTALL
IMPORT
SIG · TO-REGEX-RANGE
T
to-regex-range
serializationjavascriptv5.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

toRegexRange
import toRegexRange from 'to-regex-range';
import { toRegexRange } from 'to-regex-range';
The library primarily uses CommonJS `module.exports = function...`, so it's a default import in ESM contexts. Named import will likely result in `undefined`.
toRegexRange
const toRegexRange = require('to-regex-range');
This is the original and most direct way to import the library in Node.js CommonJS environments, as shown in its documentation.
toRegexRange
import toRegexRange = require('to-regex-range');
For TypeScript projects configured with CommonJS module resolution or needing explicit CJS import syntax for older libraries.

Demonstrates generating a regex string for a numeric range, creating a RegExp object, and testing various numbers. Also shows how to use options like `relaxZeros` for specific padding requirements.

import toRegexRange from 'to-regex-range'; // Generate a regex source string for numbers 1 to 100 const rangeSource = toRegexRange(1, 100); console.log(`Generated regex source: ${rangeSource}`); // Expected output: Generated regex source: [1-9]|[1-9][0-9]|100 // Create a RegExp object from the source, adding start/end anchors const regex = new RegExp(`^${rangeSource}$`); console.log(`Testing '5': ${regex.test('5')}`); // true console.log(`Testing '50': ${regex.test('50')}`); // true console.log(`Testing '100': ${regex.test('100')}`); // true console.log(`Testing '0': ${regex.test('0')}`); // false console.log(`Testing '101': ${regex.test('101')}`); // false console.log(`Testing '05' (zero-padded, not matched by default): ${regex.test('05')}`); // false // Example with zero-padding option for 001 to 010 const zeroPaddedSource = toRegexRange('001', '010', { relaxZeros: false }); const zeroPaddedRegex = new RegExp(`^${zeroPaddedSource}$`); console.log(`\nGenerated zero-padded regex source: ${zeroPaddedSource}`); console.log(`Testing '005': ${zeroPaddedRegex.test('005')}`); // true console.log(`Testing '010': ${zeroPaddedRegex.test('010')}`); // true
Debug
Known issues
gotchaThe `toRegexRange` function returns a regular expression *source string*, not a `RegExp` object. Developers must pass this string to `new RegExp()` themselves to create a usable regular expression.
fix
Always wrap the output in `new RegExp()`: `const regex = new RegExp(toRegexRange(min, max));`
affects: >=1.0.0
gotchaWhen using the generated regex, it's often crucial to add `^` (start of string) and `$` (end of string) anchors to ensure the regex matches the entire input string, rather than just a substring. Without anchors, `new RegExp(toRegexRange('1', '5')).test('030')` would return `true` because '3' is in the range.
fix
Apply anchors to the generated source: `const regex = new RegExp(`^${toRegexRange(min, max)}$`);`
affects: >=1.0.0
gotchaBy default, `to-regex-range` relaxes matching for leading zeros when ranges are zero-padded (e.g., `toRegexRange('-0010', '0010')`). If strict zero-padding is required, the `relaxZeros: false` option must be used.
fix
For strict zero-padding, pass `{ relaxZeros: false }` in the options object: `toRegexRange('001', '010', { relaxZeros: false });`
affects: >=2.0.0
gotchaThe library does not support 'steps' or 'increments' for ranges (e.g., matching only even numbers within a range). It generates a regex for every number within the min/max boundary.
fix
For stepped ranges, additional filtering or a different approach is needed after matching the broad range, or a custom regex must be constructed manually.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: toRegexRange is not a function
Attempting a named import in an ESM module when the library exports a default function (CommonJS `module.exports`).
fix
Use a default import: `import toRegexRange from 'to-regex-range';` or CommonJS `const toRegexRange = require('to-regex-range');`
Regex unexpectedly matches substrings, not entire numbers.
Forgetting to apply `^` and `$` anchors to the generated regex string before creating the `RegExp` object.
fix
Ensure the generated source is wrapped with anchors: `const regex = new RegExp(`^${toRegexRange(min, max)}$`);`
Regex does not match zero-padded numbers when expected, or matches non-zero-padded numbers when strict padding is desired.
Incorrect use or understanding of the `relaxZeros` option, which defaults to `true` to allow flexible zero-padding matching.
fix
Adjust the `relaxZeros` option in the options object, typically setting `relaxZeros: false` for strict zero-padding: `toRegexRange('001', '010', { relaxZeros: false });`
Upgrade
Version history
5.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
OpenAI (training)
1
Resources
to-regex-range — npm install to-regex-range · libregistry