Highlight custom regular expression pattern matches in the DOM, with support for capture groups, cross-element search, and the CSS Custom Highlight API.
Use this file to discover all available pages before exploring further.
markRegExp() highlights matches for a custom regular expression. The regex must have the g flag; for backward compatibility it is recompiled internally with the g flag if missing. This method is ideal when you need pattern-based matching beyond what mark() supports — for example matching HTML tags, structured tokens, or capturing specific groups independently.
The regular expression to match against the context text. Must include the g flag — the library manipulates lastIndex internally and requires it. If the g flag is absent it is recompiled automatically (backward-compatibility shim).When using separateGroups, the d flag is also required to enable match indices (hasIndices).
// Correct: g flag presentconst regex = /\b\w{5,}\b/gi;// separateGroups requires g + d flagsconst groupRegex = /(\w+)\s+(\w+)/gd;
The number of leading capture groups to skip when determining what text to highlight. The library highlights the first non-ignored group.
// Regex: /(-)(\\w+)\\s+/g with ignoreGroups: 1// Group 1 (-) is skipped; group 2 (\\w+) is highlightedinstance.markRegExp(/(-)(\w+)\s+/g, { ignoreGroups: 1 });
When true, each capturing group in the regex is highlighted as an independent match rather than highlighting the entire match string. Requires the d flag (hasIndices) on the regex.Combine with wrapAllRanges: true to handle overlapping or nested groups.
Enables marking of nesting or overlapping capturing groups. Useful when separateGroups is true and groups can overlap each other within a single match.
When using the Highlight API with acrossElements: true, creates a single range spanning the full cross-element match (true) or separate per-node ranges (false).
A callback invoked for every potential match, allowing you to accept or reject it. When acrossElements is true and a match spans several nodes, the callback is called once per participating text node (FAE).
nodeOrArray (Text | Text[]) — the text node containing the match, or an array of nodes when the Highlight API is used with acrossElements and rangeAcrossElements.
matchString (string) — the matched string:
Without ignoreGroups or separateGroups — the full regex match.
With ignoreGroups — the content of the first non-ignored capture group.
With separateGroups — the content of the current group being processed.
matchesSoFar (number) — total number of accepted matches so far.
info (object):
match (RegExpExecArray) — raw result of RegExp.exec().
count (number) — number of matches already wrapped/registered (MC).
matchStart (boolean) — true on the first node of a cross-element match (AE only).
groupIndex (number) — index of the current capture group being highlighted (SG only).
abort (boolean) — set to true to stop all further processing immediately.
The filter and each callbacks share the same info object; updates in filter are visible in each.
const instance = new Mark(document.querySelector('.content'));// Highlight 5+ letter words, case-insensitiveinstance.markRegExp(/\b\w{5,}\b/gi);
AE — property only available when acrossElements: true. SG — property only available when separateGroups: true. AE SG — only available when both options are enabled. MC — count reflects matches already wrapped in HTML elements or registered as Highlight API ranges at the time the callback fires.
The separateGroups option requires the d (hasIndices) flag on your regex. Without it, group boundary positions cannot be determined and the option will not function correctly.