Skip to content

regexp

Validates that a field value matches a regular expression.

Options

OptionTypeDefaultDescription
regexpstring | RegExprequiredThe regular expression to match against
flagsstring""Flags applied when regexp is a string (e.g. "i" for case-insensitive)
messagestringlocale defaultCustom error message

Playground

Valid values

ValueNotes
"abc123"Matches ^[a-z0-9]+$
"my-slug"Matches ^[a-z0-9-]+$

Invalid values

ValueReason
"ABC!"Uppercase and special char fail ^[a-z0-9]+$
"has space"Space not in pattern

Notes

  • Empty string ("") always returns valid: true — validators only run on non-empty values. Combine with notEmpty to require a value.
  • regexp can be a string ("^[0-9]+$") or a RegExp literal (/^[0-9]+$/).
  • flags only applies when regexp is a string; RegExp literals already carry their flags.

Released under the MIT License.