Skip to content

Instantly share code, notes, and snippets.

View MAZ01001's full-sized avatar
🪑
Nightwatch

MAZ MAZ01001

🪑
Nightwatch
View GitHub Profile
@MAZ01001
MAZ01001 / regexp-engine-base.js
Created March 28, 2026 18:31
Simple RegExp engine in JavaScript for understanding backtracking in RegExp via try-catch
// got the idea while answering https://stackoverflow.com/questions/79534604/regular-expressions-loop-code-how-backtracking-works
/**
* @callback MatchCallback sub-match
* @param {string | ArrayLike<string>} text - string or list of characters (`{length: number, [index: number]: string}`)
* @param {number} index - index into `text` (safe integer; can be out of range)
* @returns {number} new `index` (safe integer; unchanged if empty match)
* @throws {TypeError} if {@linkcode index} is not a safe integer
* @throws {TypeError} if {@linkcode text} is not a string nor a list of characters (`{length: number, [index: number]: string}` and entries are not empty)
* @throws {typeof Backtracking} if match fails ! (other errors should be thrown again and not silenced)