gpt4 book ai didi

regex - 为什么正则表达式匹配在 nodejs 中返回的值比正常值多?

转载 作者:搜寻专家 更新时间:2023-10-31 23:42:55 25 4
gpt4 key购买 nike

如果我在 Node repl中执行这条指令

"hello".match(/(\w+)(.*)/)

它返回这个

[ 'hello',
'hello',
'',
index: 0,
input: 'hello' ]

我预计它会返回前三项,其他值从何而来?

最佳答案

  • 数组中的第一项是整个正则表达式匹配项(“组 0”)。当然是你好
  • 第二项是第一个捕获组匹配(\w+)的内容。又是你好
  • 第三项是第二个捕获组匹配(.*)的内容。这是 hello 之后的空字符串。
  • index 是匹配开始的位置 - 这是字符串的第一个字符。
  • input 显示执行正则表达式的字符串 - 这是 hello

找到这方面的文档非常困难(至少对我而言),但这里有一些来自 MSDN 的内容描述了正则表达式匹配返回的对象:http://msdn.microsoft.com/en-us/library/ie/7df7sf4x(v=vs.94).aspx :

If the global flag is not set, the array returned by the match method has two properties, input and index. The input property contains the entire searched string. The index property contains the position of the matched substring within the complete searched string.

关于regex - 为什么正则表达式匹配在 nodejs 中返回的值比正常值多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24414015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com