Skip to content

createRegexMatcher

createMatcher with every string read as the source of a regular expression, without slashes or flags ('^(Home|Login)$'): for a setting documented as “a regular expression”.

Available since v3.1.5

import { createRegexMatcher } from '@helpers4/string';
// or, from the all-in-one package (same code, one install):
import { createRegexMatcher } from 'helpers4/string';
createRegexMatcher(pattern: MatcherPattern | readonly MatcherPattern[], options: Omit<MatcherOptions, "syntax">): function
ParameterTypeDescription
patternMatcherPattern | readonly MatcherPattern[]A pattern, or an array of patterns and `!` exclusions
optionsOmit<MatcherOptions, "syntax">Case sensitivity (adds the `i` flag when `false`)

function — A function telling whether a string matches

A setting documented as a regular expression

Section titled “A setting documented as a regular expression”

The string is the source, without slashes.

createRegexMatcher('^(Home|Login)$')('Login') // => true

caseSensitive: false adds the i flag.

createRegexMatcher('^icon-\\d+$', { caseSensitive: false })('Icon-42') // => true

Options for createMatcher and createSafeMatcher.

interface MatcherOptions {
  caseSensitive?: boolean;
  extended?: boolean;
  syntax?: "auto" | "regex" | "glob";
}

One pattern accepted by createMatcher: a string (read according to MatcherOptions.syntax), a RegExp, or a predicate of your own.

type MatcherPattern = string | RegExp | ((text: string) => boolean)

View source on GitHub