标题: ChatGPT之JavaScript非捕获组 创建: 2023-02-20 15:25 链接: https://scz.617.cn/web/202302201525.txt 参看 《新浪微博批量拉黑/解除拉黑脚本》 https://scz.617.cn/web/202208141754.txt 第一个脚本出现了JavaScript非捕获组 -------------------------------------------------------------------------- /* * What does ?: do in regex - [2010-09-14] * https://stackoverflow.com/questions/3705842/what-does-do-in-regex * * 脚本中的"?:"好像未达预期目的 */ var temp = resp.data.html.match( /(?:uid=\")(\d+)(?:\")/g ); -------------------------------------------------------------------------- 放狗查(?:)的含义,stackoverflow的回答与Chrome F12中执行结果不符,下面是其 简化示例 -------------------------------------------------------------------------- var text = 'prefix uid="1234" postfix'; var temp = text.match( /(?:uid=\")(\d+)(?:\")/g ); console.log( temp ); -------------------------------------------------------------------------- Chrome F12输出 ['uid="1234"'] temp的值是uid="1234",而非"1234",未体现「非捕获组」的效果。 ChatGPT给了一个非捕获组示例 -------------------------------------------------------------------------- var text = "Here are some links to https://www.google.com and https://www.github.com"; var temp = text.match( /(?:https:\/\/)(\S+)/g ); console.log( temp ); -------------------------------------------------------------------------- ChatGPT声称其输出 ["www.google.com", "www.github.com"] Chrome F12输出 ["https://www.google.com", "https://www.github.com"] 问ChatGPT为啥Chrome的执行结果与它声称的不符,它说它在Node.js v16.14.0中测 的,Chrome所用JavaScript引擎与Node.js并不等价,然后ChatGPT提供了修改版 -------------------------------------------------------------------------- var text = "Here are some links to https://www.google.com and https://www.github.com"; var temp = Array.from( text.matchAll( /(?:https:\/\/)(\S+)/g ), m => m[1] ); console.log( temp ); -------------------------------------------------------------------------- Chrome F12输出 ["www.google.com", "www.github.com"] 我没有Node.js v16.14.0环境,在Ubuntu 22中 $ aptitude install nodejs $ node -v v12.22.9 $ node some.js Node.js v12.22.9的反应同Chrome F12,与ChatGPT声称的Node.js v16.14.0不同。 ChatGPT算得上生产力工具,像我对JavaScript基本文盲,让我去一条条翻语言手册 不现实,直接放狗,有时英文描述起来费劲,而ChatGPT对话有上下文,贴近自然语 言,像个助教一样。前述修改版不是我要求的,只是在有上下文的对话里质疑它,它 就理解了我在干啥,并提供修改版。