惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Last Week in AI
Last Week in AI
IT之家
IT之家
A
About on SuperTechFans
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
V
Visual Studio Blog
Microsoft Security Blog
Microsoft Security Blog
博客园_首页
aimingoo的专栏
aimingoo的专栏
The Cloudflare Blog
Vercel News
Vercel News
博客园 - Franky
有赞技术团队
有赞技术团队
B
Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
量子位
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog

正则表达式

增加一点动力学正则 求教,如何系统性的学习正则表达式? sigil 正则表达式疑问 editplus 中如何去掉空格 临门一脚,这个正则怎么匹配不上字符串啊? 正则表达式合集帖(希望本帖能存活十年以上) 正则如何完整匹配整个单词 请问 EM 正则如何在文章最前面添加一段话 求教,这个正则怎么写 这个可以用正则表达式做到吗?GPT 聊了半天了 [求助]如何利用正则表达式将文本或换行用 p 标签包裹起来 正则匹配,怎么匹配不定内容的成对的中间的内容呢? 正则表达式 locate --regex 'bin/(bz|gz|zip)'有没语法问题? 上下两行内容合并正则有办法能实现吗 正则表达式如何截取一个完整括号的内容 有对 posix-正则 有研究的吗?问下 editplus 求个全选正则忽略前面的符号及符号内的内容 各位老哥,正则怎么写能排除一个或多个特定的 IP,匹配剩余所有 IP(或者说是否可以实现) 想请教一个正则表达式,这正则能做吗 使用正则.*ab.*搜索二进制文件(pdf 格式),搜不到 ab 但是.*a.*可以找到 a,除了 ab 还有别的正则写法么? 这个正则表达式怎么写,要么在开头,要么前面有空格 求助:正则获取 sql 语句中的表名 Safari 上不支持向后匹配的正则表达式,请教替代方法 正则表达式如何忽略子字符串中的内容 求助 nginx 的 rewrite,需求是把 http 流量转到 https 求大佬指点 iHateRegex 一个收录各种正则表达式的网站 请教一下,如何利用正则表达将 html 中在<p></p>之间的/筛选出来 ep 求一个数字和单位的正则 正则菜鸟求教
/(\w*)*$/.test('aaa#')这个正则导致我们的页面炸了……不同语...
phpfpm · 2024-03-01 · via 正则表达式

-2 本文用到的相关工具的版本

  • Chrome=122.0.6261.69
  • Nodejs=v10.19.0
  • PHP 7.4.3-4ubuntu2.20 (cli) (built: Feb 21 2024 13:54:34) ( NTS )
  • Python 3.8.10
  • go version go1.13.8 linux/amd64

(不要吐槽和讨论版本,除非你确定这玩意在新版本上没问题,生产环境随便找台机器测的)

-1 这玩意哪来的?

这玩意是我们前端同学问 GPT ,如何写一个匹配网址的正则问到的。

(/^( https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/).test('https://foo.com/a-long-url-contains-path-and-query-and-anchor/foo/bar/baz?boo=baa#anchor');

*** (这个真的可以执行,建议新窗口 F12 试下) ***

于是,真的匹配一段文本的时候,就导致浏览器卡死了,无法做后续渲染,在 profiling 的时候查到是这个正则的问题。

0 MVP 测一下

function testRegexPerformance(repeatCount) {
    var testString = 'a'.repeat(repeatCount) + '#';
    var regex = /(\w*)*$/;

    var startTime = process.hrtime();
    var result = regex.test(testString);
    var endTime = process.hrtime(startTime);
    var executionTime = endTime[0] * 1000 + endTime[1] / 1000000;

    console.log("Repeat Count:", repeatCount);
    console.log("Execution Time:", executionTime + " milliseconds");
    console.log("-----------------------------------" + result);
}

// 测试从 1 到 50 的重复次数
for (var i = 1; i <= 50; i++) {
    testRegexPerformance(i);
}
Repeat Count: 20
Execution Time: 35.191223 milliseconds
-----------------------------------true
Repeat Count: 21
Execution Time: 71.355698 milliseconds
-----------------------------------true
Repeat Count: 22
Execution Time: 140.852157 milliseconds
-----------------------------------true
Repeat Count: 23
Execution Time: 287.687666 milliseconds
-----------------------------------true
Repeat Count: 24
Execution Time: 577.368917 milliseconds
-----------------------------------true
Repeat Count: 25
Execution Time: 1148.243059 milliseconds
-----------------------------------true
Repeat Count: 26
Execution Time: 2297.804939 milliseconds

i=25的时候,执行时间就到了秒级,之后都是指数级增长。

1 结果是 true 是符合预期的

*表示 0 个或者多个,没有任何一个\w 也是没问题的

2 Regexp.test vs String.match

# 不匹配
> 'a'.match(/(b)/)
null

# 匹配
> 'a'.match(/(b)/)
null

# 匹配
> 'aa'.match(/(a)/)
[ 'a', 'a', index: 0, input: 'aa', groups: undefined ]

# 不那么匹配
> 'aaa#'.match(/(\w*)*$/)
[ '', undefined, index: 4, input: 'aaa#', groups: undefined ]

# 匹配?
> /(\w*)*$/.test('aaa#')
true
>

起因是我旁边的同学说.net 没有 test ,只有 match ,而且结果是 false

所以,js 里面如果用 match 试下,大概有三种结果:

  • 匹配:test=true
  • 不匹配:test=false
  • 不太匹配:test=true ,但是 match[0]是空,1 是undefined

3 其他语言的表现?

  • js:匹配,衰减

  • PHP: 不匹配,不衰减

  • Python:None(不匹配),衰减

  • Go: 匹配,不衰减

  • F#: 匹配,衰减

4 所以是为啥?

二楼放测试程序,不占地儿了