pattern
The string matches a regular expression.
Signature
ts
function pattern(re: RegExp, msg?: Message): Action<string>Parameters
| Parameter | Type | Description |
|---|---|---|
re | RegExp | Regular expression |
msg | Message | Custom error message. A string with {params} or a function |
Error codes
| Code | When | Default message |
|---|---|---|
pattern | The string does not match | Invalid format |
You can change the texts: see Messages & i18n.
Notes
- Use
^and$: without them/\d+/accepts'abc1', because it finds a digit inside the string. - The
gflag is safe —lastIndexis reset before each check.
Examples
Phone
ts
pipe(string(), pattern(/^\+1\d{10}$/, 'Format: +1XXXXXXXXXX'))Slug
ts
pipe(string(), pattern(/^[a-z0-9]+(-[a-z0-9]+)*$/))