Regular expressions with a global flag are not working right. The code uses regex.test() and regex.exec(), both increasing an internal lastIndex counter.
For example:
<Highlight search=/brown|jumps/g">The quick brown fox jumps over the lazy dog</Highlight>
will only highlight 'jumps' because the first conditional search.test(remainingCleaned) will move the lastIndex counter right after the 'brown' keyword. search.exec(subject) will then only find 'jumps'.