Skip to content

toRegExp

The toRegExp() function normalizes a string, string array, or RegExp into a regular expression. When the input is textual, it escapes regex metacharacters and builds an exact-match pattern.

Interactive example

Syntax

typescript
function toRegExp(
	input: string | string[] | RegExp
): RegExp;

Parameters

  • input : Value to convert into a RegExp.

Return value

A RegExp:

  • from a string: exact literal match (^...$)
  • from a string[]: exact alternatives (^(?:...|...)$)
  • from a RegExp: the same instance

See also

Released under the MIT license.