you have 2 regexps, one matches equations with an index
on matches equations without an index,
xxxxxx (1)
or
xxxxxx
the code prioritize the first regexp over the second, then you have more than one lines of equations like:
aaaaa
.... some text ...
bbbbb (1)
Javascript tends to match the longest string possible, so it will treat the above as a single equation, hence the bug.
The fix adds a negative lookahead to exclude \[ \] patterns from an equation match. This will change the regexp capture index. so I also changed res[2] to res[res.length -1]
you have 2 regexps, one matches equations with an index on matches equations without an index,
xxxxxx (1)
or
xxxxxx
the code prioritize the first regexp over the second, then you have more than one lines of equations like:
aaaaa
.... some text ...
bbbbb (1)
Javascript tends to match the longest string possible, so it will treat the above as a single equation, hence the bug.
The fix adds a negative lookahead to exclude \[ \] patterns from an equation match. This will change the regexp capture index. so I also changed res[2] to res[res.length -1]