@aritro2002/regex
v0.1.0
Published
Reusable, anchored regular expressions for common validation
Readme
@aritro2002/regex
Anchored, reusable regular expressions for everyday validation, plus a couple of string helpers.
Install
npm install @aritro2002/regexUsage
import { isEmail, isSemver, classify, escape, toSlug } from '@aritro2002/regex';
isEmail('[email protected]'); // true
isEmail('[email protected] extra'); // false — patterns are anchored
isSemver('1.2.3-beta.1'); // true
classify('my-post'); // ['slug']
escape('1+1'); // '1\\+1'
toSlug(' Héllo, World! '); // 'hello-world'Patterns
email, url, slug, hexColor, ipv4, uuid, isoDate, semver.
Each is exposed as a factory on patterns, not a shared instance:
import { patterns } from '@aritro2002/regex';
patterns.email().test('[email protected]');A single shared regex would carry lastIndex between calls if it ever gained
the g flag, which makes validation return alternating results. Handing out a
fresh instance per call rules that out.
API
| Export | Description |
| --- | --- |
| patterns | Map of pattern name to a factory returning a fresh RegExp. |
| patternNames | Array of every pattern name. |
| matches(name, value) | Tests a value against a named pattern. |
| isEmail … isSemver | One shorthand per pattern. |
| classify(value) | Every pattern name the value satisfies. |
| escape(value) | Escapes a string for use inside a RegExp. |
| toSlug(value) | Turns arbitrary text into a URL-safe slug. |
License
MIT
