Skip to main content
A RegExp in TypeScript is an object representing a regular expression, utilized for pattern matching and text manipulation within strings. TypeScript provides static typing over the native JavaScript RegExp implementation through built-in interfaces such as RegExp, RegExpMatchArray, and RegExpExecArray, ensuring type safety when interacting with match results, indices, and capture groups.

Instantiation

Regular expressions in TypeScript can be instantiated using either literal syntax or the RegExp constructor. The literal syntax is compiled when the script is loaded, whereas the constructor is compiled at runtime.

TypeScript Core Interfaces

TypeScript defines specific return types for regular expression operations to handle the complex arrays returned by the underlying JavaScript engine.
  • RegExp: The interface representing the regular expression instance.
  • RegExpExecArray: An array type returned by RegExp.prototype.exec(). It extends Array<string> and includes the required properties index (number) and input (string), alongside an optional groups property ({ [key: string]: string } | undefined) which is populated only when named capture groups are present.
  • RegExpMatchArray: An array type returned by String.prototype.match(). It extends Array<string> but differs from RegExpExecArray by typing the index and input properties as optional (index?: number, input?: string). This distinction accounts for the runtime behavior where match() omits these properties when the global (g) flag is applied.

RegExp Instance Methods

The RegExp object exposes two primary methods for pattern evaluation. test() Evaluates the string against the pattern and returns a boolean indicating whether a match exists.
exec() Executes a search for a match in a specified string. Returns a RegExpExecArray if successful, or null if it fails. When the g (global) or y (sticky) flags are present, exec() updates the lastIndex property of the RegExp instance, allowing for stateful iteration.

String Methods Utilizing RegExp

TypeScript strongly types the String prototype methods that accept RegExp arguments.

Flags and Modifiers

TypeScript supports standard ECMAScript regular expression flags. The presence of certain flags alters the expected return types or available properties.
  • g (Global): Finds all matches rather than stopping after the first.
  • i (Ignore case): Makes matches case-insensitive.
  • m (Multiline): Changes behavior of ^ and $ to match the start/end of lines.
  • s (DotAll): Allows . to match newline characters.
  • u (Unicode): Treats patterns as sequences of Unicode code points.
  • y (Sticky): Matches only from the index indicated by the lastIndex property.
  • d (HasIndices): Generates match indices.
The d Flag (Indices) When the d flag is utilized, TypeScript recognizes the indices property on the resulting RegExpExecArray. This requires the es2022.regexp library in the tsconfig.json.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More