繁体   English   中英

复杂的正则表达式捕获-Javascript

[英]Complicated Regex Capturing - Javascript

我一直在尝试捕捉下面的部分

a1A ^(?\..*+) and a1A ^(?\..*+) g:a1A ^(?\..*+)

但是,对于我要实现的目标,我无法创建100%工作的正则表达式。

正则表达式必须捕获在字母后跟冒号的所有内容,并且在冒号之前的字母也要捕获,然后必须在捕获第一个匹配的字母后捕获的内容,然后是冒号,也要捕获在冒号之前的所有字母,直到空格为止。 例如下面的这一行:

x:a1A ^(?\..*+) z:a1A ^(?\..*+) g:a1A ^(?\..*+)

--> captured value must be 'x', 'a1A ^(?\..*+)', 'z', and 'a1A ^(?\..*+) g:a1A ^(?\..*+)'

另一个例子

x23:a1A ^(?\..*+) z:a1A ^(?\..*+) g:a1A ^(?\..*+)

--> captured value must be 'x23', 'a1A ^(?\..*+)', 'z' and 'a1A ^(?\..*+) g:a1A ^(?\..*+)'

然后,如果没有以任何字母开头并以冒号开头的字符串,则它必须捕获所有内容,直到空格后接一个或多个字母和冒号为止。 例如

a1A ^(?\..*+) s:a1A ^(?\..*+)

--> captured value must be 'a1A ^(?\..*+)', 's' and 'a1A ^(?\..*+)

我想出的正则表达式是?:([^: ]+):)?([^ ]+ [^ ]+)(?: |$)

(?:([^: ]+):)?  // don't capture the colon, capture the part before the colon
([^ ]+ [^ ]+)   // capture the two groups that may or may not be preceded by the colon
(?: |$)         // end at either a space or the end of the line/string

这是我测试的小提琴: http : //jsfiddle.net/9nbsu84v/

暂无
暂无

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

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