I need to find the most efficient way of matching multiple regular expressions on a single block of text. To give an example of what I need, consider a block of text:
<
If the question is how to replace multiple generic patterns with corresponding replacements - either strings or functions, it's quite tricky because of special characters, capturing groups and backreference matching.
You can use https://www.npmjs.com/package/union-replacer for this exact purpose. It is basically a string.replace(regexp, string|function)
counterpart, which allows multiple replaces to happen in one pass while preserving full power of string.replace(...)
.
Disclosure: I am the author and the library was developed because we had to support user-configured replaces.