Coincidencia de construcciones anidadas en JavaScript, parte 2

Cuando publiqué mi función matchRecursive el otro día (que permite hacer coincidir fácilmente construcciones anidadas), noté que podría modificarse fácilmente para que funcione con un patrón de expresión regular en lugar de una cadena como format argumento. Después de mirarlo de nuevo, me di cuenta de que la conversión no sería del todo sencilla, así que seguí adelante y lo reimplementé como matchRecursiveRegExp para trabajar con delimitadores de formato regex. Ahora puede usarlo para dar me gusta, p. matchRecursiveRegExp(str, "<div\\b[^>]*>", "</div>", "gi") para hacer coincidir todo el contenido de todos los <div> más externos etiquetas.

Como antes, lea los comentarios del código para obtener más detalles. Tenga en cuenta que en esta versión he hecho que los nombres de las variables sean más o menos indescifrables para mantener el tamaño del código bajo, así que vea la función anterior para una implementación similar pero más legible.

// (c) 2007 Steven Levithan <stevenlevithan.com>
// MIT License

/*** matchRecursiveRegExp
	Accepts a string to search, a left and right format delimiter
	as regex patterns, and optional regex flags. Returns an array
	of matches, allowing nested instances of left/right delimiters.
	Use the "g" flag to return all matches, otherwise only the
	first is returned. Be careful to ensure that the left and
	right format delimiters produce mutually exclusive matches.
	Backreferences are not supported within the right delimiter
	due to how it is internally combined with the left delimiter.
	When matching strings whose format delimiters are unbalanced
	to the left or right, the output is intentionally as a
	conventional regex library with recursion support would
	produce, e.g. "<<x>" and "<x>>" both produce ["x"] when using
	"<" and ">" as the delimiters (both strings contain a single,
	balanced instance of "<x>").

	examples:
		matchRecursiveRegExp("test", "\\(", "\\)")
			returns: []
		matchRecursiveRegExp("<t<<e>><s>>t<>", "<", ">", "g")
			returns: ["t<<e>><s>", ""]
		matchRecursiveRegExp("<div id=\"x\">test</div>", "<div\\b[^>]*>", "</div>", "gi")
			returns: ["test"]

*/
function matchRecursiveRegExp (str, left, right, flags) {
	var	f = flags || "",
		g = f.indexOf("g") > -1,
		x = new RegExp(left + "|" + right, "g" + f),
		l = new RegExp(left, f.replace(/g/g, "")),
		a = [],
		t, s, m;

	do {
		t = 0;
		while (m = x.exec(str)) {
			if (l.test(m[0])) {
				if (!t++) s = x.lastIndex;
			} else if (t) {
				if (!--t) {
					a.push(str.slice(s, m.index));
					if (!g) return a;
				}
			}
		}
	} while (t && (x.lastIndex = s));

	return a;
}

Puedes descargarlo aquí.

Avíseme si hay alguna característica adicional que le gustaría ver con esto.