Skip to content

matchOr

ts
function String.matchOr<T>(
    target: string,
    source: string | RegExp,
    or: T,
): RegExpMatchArray | T

Returns the result of matching target string against source string or regular expression, or the or value if no match is found.

Example

ts
import { String } from "@monstermann/string";

String.matchOr("hello world", "world", []); // ["world", index: 6, input: "hello world", groups: undefined]
String.matchOr("hello world", /\d+/, []); // []
ts
import { String } from "@monstermann/string";

pipe("hello world", String.matchOr("world", [])); // ["world", index: 6, input: "hello world", groups: undefined]
pipe("hello world", String.matchOr(/\d+/, [])); // []