Replacing multiple patterns in a block of data

后端 未结 4 754
遇见更好的自我
遇见更好的自我 2020-12-31 14:48

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:

<
4条回答
  •  执念已碎
    2020-12-31 15:30

    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.

提交回复
热议问题