简体   繁体   中英

Complicated Regex Capturing - Javascript

I've been trying to capture the part below

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

However I can't create a 100% working regex for what I am trying to achieve.

The regex must capture anything after the letters followed by a colon in the beginning also the letters before the colon and then it must capture after the first matching of letters followed by a colon and also the letters before the colon till space. eg for this line below:

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

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

another example

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

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

and then in case there is no string starting with any letter and followed by a colon, then it must capture anything till the space followed by a letter or letters and a colon. eg

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

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

The regex I came up with is ?:([^: ]+):)?([^ ]+ [^ ]+)(?: |$) .

(?:([^: ]+):)?  // 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

Here is a fiddle where I tested it out: http://jsfiddle.net/9nbsu84v/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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