【发布时间】:2022-01-21 20:09:04
【问题描述】:
在使用正则表达式匹配多个关键字的文档时,可能需要二次时间(文档数 × 关键字数)。
使用以下一组关键字:
['ant', 'antelope', 'albatross', 'alligator']
我会产生愚蠢的正则表达式:
/ant|antelope|albatross|alligator/
但我正在寻找一种将集合转换为类似内容的解决方案:
/a(nt(|elope)|l(batross|ligator))/
效率更高
【问题讨论】:
标签: javascript node.js regex performance optimization