Use multiple RegExp like you had only one ! Iterate on matches or find the first match.
This is extends from multiregexp
npm i ptm-multiregexp
pnpm i ptm-multiregexp
yarn add ptm-multiregexp
Import
import MultiRegExp from "ptm-multiregexp"; //esm
import MultiRegExp = require("ptm-multiregexp"); // typescript cjs
const MultiRegExp = require("ptm-multiregexp"); // cjs
deno add jsr:@ptm/multiregexp
Import symbol
import * as multiregexp from "@ptm/multiregexp";
Import directly with a jsr specifier
import * as multiregexp from "jsr:@ptm/multiregexp";
npx jsr add @ptm/multiregexp
Import symbol
import * as multiregexp from "@ptm/multiregexp";
yarn add jsr:@ptm/multiregexp
Using Yarn 4.8 or older
yarn dlx jsr add @ptm/multiregexp
Import symbol
import * as multiregexp from "@ptm/multiregexp";
pnpm i jsr:@ptm/multiregexp
Using pnpm 10.8 or older
pnpm dlx jsr add @ptm/multiregexp
Import symbol
import * as multiregexp from "@ptm/multiregexp";
const regexp = /(#{1,6})[ ]+(.*)/g;
const string = "## Hello World";
const multiregexp = new MultiRegExp([regexp]);
const result = multiregexp.getMatched(string);
/*
match: "## Hello World",
start: 0,
end: 14,
groupCount: 2,
group_1: "##",
group_2: "Hello World",
*/