简体   繁体   English

正则表达式忽略捕获组内的捕获组

[英]regex ignore capturing group inside capturing group

I'm trying to capture a group but at the same time ignore a sub group but need the subgroup to be true to capture it. 我正在尝试捕获一个组,但同时忽略一个子组但需要该子组才能捕获它。 For example: 例如:

/^(?:\s*)([abc])?(?:\s*)((?:d){1}[ef]*)(.*)/.exec(' a deee ghi')
// is equal to                [" a deee ghi", "a", "deee", " ghi"]
// but I really want equal to [" a deee ghi", "a", "eee", " ghi"]

// and I want this to fail:
/^(?:\s*)([abc])?(?:\s*)((?:d){1}[ef]*)(.*)/.exec(' a eee ghi')
//                            [" a eee ghi", "a", "", "eee ghi"]

I'm trying to ignore the 'd' in the capturing group but only want it captured if there's a 'd' before any 'e' s of 'f' s in the third group 我试图忽略捕获组中的'd' ,但只想在第三组'f'的任何'e'之前有'd'时捕获它

How would I do this? 我该怎么做?

EDIT: 编辑:

To clairify, I want the third group to be filled only if there's a 'd' in the string there. 为了要求,我希望第三组只有在字符串中有'd'才能填充。 else -> don't capture the 'e' s or 'f' s 否则 - >不要捕捉'e''f'

If I understand you correctly: if the d is there, then the capture-group should be eee , but if the d is not there, then the capture-group should be blank or null. 如果我理解正确:如果d在那里,那么捕获组应该是eee ,但如果d不存在,那么捕获组应该为空或空。 Is that correct? 那是对的吗? If so, you can write: 如果是这样,你可以写:

/^(?:\s*)([abc])?(?:\s*)(?:d([ef]*)|[ef]*)(.*)/.exec(' a deee ghi')

to match ([ef]*) when d is present, and [ef]* when it is not. 匹配([ef]*)d是存在,并且[ef]*时,它不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM