为什么这些JavaScript正则表达式捕获整行的括号而不是附加到单词的后缀?

为什么这些JavaScript正则表达式捕获整行的括号而不是附加到单词的后缀?,javascript,regex,Javascript,Regex,有人能告诉我为什么我的简单表达式不能捕获可选的任意长度。后缀片段跟在hello后面,匹配完整的行吗 相反,它匹配整行hello.aa.b再见,而不是捕获括号的内容 使用此代码: 我研究这个简单的表达式已经三个多小时了,我开始相信我对捕获的工作原理有一个基本的误解:不是有一个光标一个字符一个字符地抓取每一行并捕获括号内的内容吗? 请不要找我。然后是一些空间,而不是寻找。还有一些角色,最后在外面寻找那个空间重复捕获组只会捕获最后一次迭代。在重复组周围放置一个捕获组以捕获所有迭代 var suffix

有人能告诉我为什么我的简单表达式不能捕获可选的任意长度。后缀片段跟在hello后面,匹配完整的行吗

相反,它匹配整行hello.aa.b再见,而不是捕获括号的内容

使用此代码:

我研究这个简单的表达式已经三个多小时了,我开始相信我对捕获的工作原理有一个基本的误解:不是有一个光标一个字符一个字符地抓取每一行并捕获括号内的内容吗?


请不要找我。然后是一些空间,而不是寻找。还有一些角色,最后在外面寻找那个空间

重复捕获组只会捕获最后一次迭代。在重复组周围放置一个捕获组以捕获所有迭代

var suffix = line.match(/^hello((\.[^\.]*)*)\sgoodbye$/g);
if (suffix !== null)
    suffix = suffix[1].match(/(\.[^\.\s]*)/g)

我推荐。

我最初从Perl开始,然后是PHP。当我开始使用JavaScript时,我自己也曾遇到过这种情况

在JavaScript中,全局匹配不会生成多维数组。换句话说,在全局匹配中,只有匹配[0]没有子模式

请注意后缀[0]与整个字符串匹配

试试这个:

//var line = "hello goodbye";            // desired: suffix undefined
//var line = "hello.aa goodbye";         // desired: suffix[1]=.aa
var line = "hello.aa.b goodbye";         // desired: suffix[1]=.aa suffix[2]=.b

var suffix = line.match(/^hello(\.[^.]+)?(\.[^.]+)?\s+goodbye$/);
如果必须使用全局匹配,那么必须首先捕获整个字符串,然后运行第二个正则表达式来获取子模式

祝你好运 :

更新:进一步解释

如果每个字符串只有一个匹配模式,比如var line=hello.aa.b再见; 然后,您可以使用我上面发布的模式,而不使用全局修改器

如果刺有多个可匹配的图案,请查看以下内容:

// modifier g means it will match more than once in the string
// ^ at the start mean starting with, when you wan the match to start form the beginning of the string
// $ means the end of the string
// if you have ^.....$ it means the whole string should be a ONE match
var suffix = line.match(/^hello(\.[^.]+)?(\.[^.]+)?\s+goodbye$/g);


var line = 'hello.aa goodbye and more hello.aa.b goodbye and some more hello.cc.dd goodbye';

// no match here since the whole of the string doesn't match the RegEx
var suffix = line.match(/^hello(\.[^.]+)?(\.[^.]+)?\s+goodbye$/);

// one match here, only the first one since it is not a GLOBAL match (hello.aa goodbye)
// suffix[0] = hello.aa goodbye
// suffix[1] = .aa
// suffix[2] = undefined
var suffix = line.match(/hello(\.[^.]+)?(\.[^.]+)?\s+goodbye/);

// 3 matches here (but no sub-patterns), only a one dimensional array with GLOBAL match in JavaScript
// suffix[0] = hello.aa goodbye
// suffix[1] = hello.aa.b goodbye
// suffix[2] = hello.cc.dd goodbye
var suffix = line.match(/hello(\.[^.]+)?(\.[^.]+)?\s+goodbye/g);
我希望这有帮助。

将全局标志与match方法一起使用不会返回任何捕获组。看

虽然你使用*这只是一个捕获组。*仅定义在空格出现之前内容必须匹配0次或更长时间


正如@EveryEvery所指出的,您可以使用两步方法。

有几个原因,包括如果您想要0个或更多非句点/空格字符,星号位于错误的位置,句点在捕获组中,等等。哦,天哪,超过三个小时?在我的时代,你会在一个问题上工作一周,然后才考虑到你一事无成。这一代人总是这么匆忙。戴夫,谢谢你指出这一点。理想情况下,我不想在捕获组中出现这段时间,但如果是这样的话也没关系;我无法让一个简单的捕获组工作,更不用说有嵌套括号的捕获组了。我还没有阅读规范,但我获得了match方法,在省略^$符号时返回捕获组。我真的很困惑,因为这是可行的;怎么可能呢?:line.match/\.[^\.\s]+/g@PeteAlvinhello.aa.b再见。match/\.[^\.\s]+/g返回[.aa.b]。这不是捕获组,而是匹配。如果省略了^和$符号,我使用match方法返回所有迭代:我不知道如何与您的答案相一致。有什么想法吗?line.match/\.[^\.\s]+/t谢谢!有一点我不明白。我确实必须按照你的建议实施两个步骤。一旦我有了正确的行,这个注释末尾的语句就正确地匹配了后缀,因为我可以省略^$符号。它是全局的,但它确实比匹配[0]更匹配。请帮助我,因为这与你的第二句话相矛盾。三个答案告诉了我同样的事情,但我真的不明白!line.match/\..[^\.\s]+/gOK…我将更新我的帖子,并在稍后对globalg标志与预期捕获的数量进行更多解释。
// modifier g means it will match more than once in the string
// ^ at the start mean starting with, when you wan the match to start form the beginning of the string
// $ means the end of the string
// if you have ^.....$ it means the whole string should be a ONE match
var suffix = line.match(/^hello(\.[^.]+)?(\.[^.]+)?\s+goodbye$/g);


var line = 'hello.aa goodbye and more hello.aa.b goodbye and some more hello.cc.dd goodbye';

// no match here since the whole of the string doesn't match the RegEx
var suffix = line.match(/^hello(\.[^.]+)?(\.[^.]+)?\s+goodbye$/);

// one match here, only the first one since it is not a GLOBAL match (hello.aa goodbye)
// suffix[0] = hello.aa goodbye
// suffix[1] = .aa
// suffix[2] = undefined
var suffix = line.match(/hello(\.[^.]+)?(\.[^.]+)?\s+goodbye/);

// 3 matches here (but no sub-patterns), only a one dimensional array with GLOBAL match in JavaScript
// suffix[0] = hello.aa goodbye
// suffix[1] = hello.aa.b goodbye
// suffix[2] = hello.cc.dd goodbye
var suffix = line.match(/hello(\.[^.]+)?(\.[^.]+)?\s+goodbye/g);