Skip to content

infinity.regEx

Provides functions for using Perl-compatible regular expressions (PCRE). Unlike the built-in JavaScript RegExp, these functions are also suitable for large texts.

Patterns are passed as strings without delimiters (e.g. '[0-9]+', not /[0-9]+/); backslashes have to be escaped in JavaScript string literals (e.g. '\\d+'). Options are passed as an array of option values; an option value that is not an array throws an exception. All offsets are zero-based character positions.

Module: infinity.regEx

Example:

infinity.loadModule('infinity.regEx');

let matches = infinity.regEx.matchList('([0-9]{3})-([0-9]{4})', 'Word 123-4567 word 890-1234');

console.debug(matches.length);
//2
console.debug(matches[0].text + ' at ' + matches[0].offset);
//123-4567 at 5
console.debug(matches[1].groups[1].text + ' / ' + matches[1].groups[2].text);
//890 / 1234

Interfaces

groupArray

Extends: Array<{text: string, length: number, offset: number}>

An array containing the groups of a match. Index 0 contains the whole match, indexes 1 and above the capturing groups in the order of their opening parentheses. Each entry contains the matched text, its length and its zero-based offset in the subject.


matchArray

Extends: Array<{text: string, length: number, offset: number, groups: infinity.regEx.groupArray}>

An array containing the matches. Each entry contains the matched text, its length, its zero-based offset in the subject and its groups (see groupArray).


optionArray

Extends: Array<infinity.regEx.option>

An array containing options according to the option enums.


stringArray

Extends: Array<string>

An array of strings.


Functions

extractList()

Searches the subject for all matches of the regular expression and returns, for each match, the replacement string with its backreferences resolved for that match. This is useful to extract parts of the matches, e.g. with the replacement '\\1' (or '$1') the text of the first capturing group of every match is returned. The matches are the same as those of matchList() and replace() (before 3.11 the search continued in the subject with the previous matches already replaced, which could find additional matches, e.g. with ^ and anchored). Returns an empty array if there is no match.

Signature:

extractList( regEx: string, subject: string, replacement: string, options?: infinity.regEx.optionArray ): infinity.regEx.stringArray

Parameters:

  • regEx: string

    The regular expression for the searched data.

  • subject: string

    The string inside of which the search should be performed.

  • replacement: string

    The text to be returned for each match. It can contain backreferences like \1 or $1 (capturing groups) and \0 or $0 (whole match).

  • options: infinity.regEx.optionArray, optional

    An array of additional options according to option enums.

Return type: infinity.regEx.stringArray

Example:

infinity.loadModule('infinity.regEx');
let years = infinity.regEx.extractList('([0-9]{2})\\.([0-9]{2})\\.([0-9]{4})', 'From 01.02.2024 to 31.12.2025', '$3');
console.debug(years.join(', '));
//2024, 2025

match()

Returns whether the regular expression matches anywhere inside the provided subject (unless the anchored option is used). Throws an exception if the regular expression is invalid.

Signature:

match( regEx: string, subject: string, options?: infinity.regEx.optionArray ): boolean

Parameters:

  • regEx: string

    The regular expression for the searched data.

  • subject: string

    The string inside of which the search should be performed.

  • options: infinity.regEx.optionArray, optional

    An array of additional options according to option enums.

Return type: boolean

Example:

infinity.loadModule('infinity.regEx');
if ( infinity.regEx.match('[0-9]{3}-[0-9]{4}', 'Word 123-4567 word') ) {
  //...
}

matchList()

Returns all (non-overlapping) matches of the regular expression in the provided subject, including the capturing groups of each match. Returns an empty array if there is no match.

Signature:

matchList( regEx: string, subject: string, options?: infinity.regEx.optionArray ): infinity.regEx.matchArray

Parameters:

  • regEx: string

    The regular expression for the searched data.

  • subject: string

    The string inside of which the search should be performed.

  • options: infinity.regEx.optionArray, optional

    An array of additional options according to option enums.

Return type: infinity.regEx.matchArray

Example:

infinity.loadModule('infinity.regEx');

let matches = infinity.regEx.matchList('^word', 'Word 123-4567\nword', [infinity.regEx.option.multiLine, infinity.regEx.option.caseLess]);
console.debug(matches.length);
//2

replace()

Returns the provided subject with all matches of the regular expression replaced by the replacement string.

Signature:

replace( regEx: string, subject: string, replacement: string, options?: infinity.regEx.optionArray ): string

Parameters:

  • regEx: string

    The regular expression for the searched data.

  • subject: string

    The string inside of which the search should be performed.

  • replacement: string

    The text to put instead of each match. It can contain backreferences like \1 or $1 (capturing groups) and \0 or $0 (whole match).

  • options: infinity.regEx.optionArray, optional

    An array of additional options according to option enums.

Return type: string

Example:

infinity.loadModule('infinity.regEx');

let result = infinity.regEx.replace('[0-9]', 'Word 123-4567 word', 'x');
console.debug(result);
//Word xxx-xxxx word

result = infinity.regEx.replace('([0-9]{3})-([0-9]{4})', 'Word 123-4567 word', '$2-$1');
console.debug(result);
//Word 4567-123 word

Enums

infinity.regEx.option

Values:

  • caseLess: 0

    Case-insensitive matching (PCRE i).

  • multiLine: 1

    ^ and $ match at the start and end of every line, not only at the start and end of the subject (PCRE m).

  • singleLine: 2

    . also matches line breaks (PCRE s, "dot all").

  • extended: 3

    Whitespace in the pattern is ignored and # starts a comment up to the end of the line (PCRE x).

  • anchored: 4

    The pattern only matches at the start of the subject (or, for subsequent matches, directly after the previous match). It does not force a match up to the end of the subject.

  • unGreedy: 5

    Inverts the greediness of quantifiers: they are lazy by default and become greedy when followed by ?.

  • noAutoCapture: 6

    Plain parentheses (...) do not capture (they behave like (?:...)); named groups (?<name>...) still capture.

Example:

infinity.loadModule('infinity.regEx');
let option = infinity.regEx.option.caseLess;