Skip to main content

Assertions

Anchors

Anchors are special characters or sequences that specify positions in the input string rather than matching specific characters.

Start and end of string

const startOfString: RegexConstruct;
const endOfString: RegexConstruct;
  • startOfString anchor matches the start of a string (or line, if multiline mode is enabled). Regex syntax: ^.
  • endOfString anchor matches the end of a string (or line, if multiline mode is enabled). Regex syntax: $.

Word boundary

This API was added in version 1.3.0.

const wordBoundary: RegexConstruct;
const nonWordBoundary: RegexConstruct;
  • wordBoundary matches the positions where a word character is not followed or preceded by another word character, effectively indicating the start or end of a word. Regex syntax: \b.
  • nonWordBoundary matches the positions where a word character is followed or preceded by another word character, indicating that it is not at the start or end of a word. Regex syntax: \B.

Note: word characters are letters, digits, and underscore (_). Other special characters like #, $, etc are not considered word characters.

Lookarounds

Lookarounds in regex are used for asserting that some pattern is or isn't followed or preceded by another pattern, without including the latter in the match.

lookahead()

This API was added in version 1.3.0.

function lookahead(sequence: RegexSequence): RegexConstruct;

Regex syntax: (?=...).

Allows for conditional matching by checking for subsequent patterns in regexes without consuming them.

negativeLookahead()

This API was added in version 1.3.0.

function negativeLookahead(sequence: RegexSequence): RegexConstruct;

Regex syntax: (?!...).

Allows for matches to be rejected if a specified subsequent pattern is present, without consuming any characters.

lookbehind()

This API was added in version 1.3.0.

function lookbehind(sequence: RegexSequence): RegexConstruct;

Regex syntax: (?<=...).

Allows for conditional matching by checking for preceeding patterns in regexes without consuming them.

negativeLookbehind()

This API was added in version 1.3.0.

function negativeLookahead(sequence: RegexSequence): RegexConstruct;

Regex syntax: (?<!...).

Allows for matches to be rejected if a specified preceeding pattern is present, without consuming any characters.