我正在try 使用以下正则表达式获取匹配的单词以及前后的一些单词(最多5个):

const start = Date.now();
console.log("There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).".match(/(\S*\s*){0,5}lorem(\s*\S*){0,5}/giu));

console.log(Date.now() - start);

然而,这似乎非常慢,而且需要数百毫秒的时间.我是不是遗漏了什么,在性能方面还能改进吗?

推荐答案

看起来操作员想要在lorem的前面和后面加5个单词.因此,如果我们将\S*\s*更改为\S+\s+,则正则表达式的速度要快lorem倍.此外,OP的正则表达式失败,并带有单引号:

and a search for 'lorem ipsum' will uncover many web

我的正则表达式也失败了,因此添加了\S*lorem\S*.

此外,我们还可以省略捕捉单词.

所以最后的正则表达式是:

/(?:\S+\s+){0,5}\S*lorem\S*(?:\s+\S+){0,5}/giu

enter image description here

<script benchmark data-count="100">

const str = "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). and lorem again.";

// @benchmark original
str.match(/(\S*\s*){0,5}lorem(\s*\S*){0,5}/giu)

// @benchmark Alexander
str.match(/(?:\S+\s+){0,5}\S*lorem\S*(?:\s+\S+){0,5}/giu)

</script>
<script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>

Javascript相关问答推荐

有没有方法在Angular中的bowser选项卡之间移动HTML?

如何计算数组子级的深度- Vue.js

如何在ReactJS中修复这种不需要的按钮行为?

事件错误:类型错误:无法读取未定义的属性(读取stopPropagation)

访客柜台的风格React.js

设置默认值后动态更新japbox或调色板

Vue Quill css仅应用于我的第一个Quill Editor组件+如何自定义工具栏

为什么使用MAX_SAFE_INTEGER生成随机整数时数字分布不准确?

如何使用侧边滚动按钮具体滚动每4个格?

从mat—country—select获取整个Country数组

使用javascript将Plotly Expandable Sparkline转换为HighCharter Plot在bslib卡中

显示图—如何在图例项上添加删除线效果?

使用JQuery单击元素从新弹出窗口获取值

VUE 3捕获错误并呈现另一个组件

如何在 Select 文本时停止Click事件?

搜索功能不是在分页的每一页上进行搜索

当我try 将值更改为True时,按钮不会锁定

将对象推送到数组会导致复制

我正在试着做一个TicTacToe Ai来和我玩.但是,我试着在第一个方块被点击时出现一个X,然后在第二个方块之后出现一个O

是否可以在不更改组件标识的情况下换出Reaction组件定义(以维护状态/引用等)?如果是这样的话,是如何做到的呢?