Regex capturing group with many repeating white spaces

Viewed 67

I have an issue capturing this kind of groups where:

a1:b1 a2:b2 b3 b4 a3:b5

and the expected result is

a1:b1
a2:b2 b3 b4
a3:b5

the best expression I could get is /[\w]*:([\w]*)/g and this is what i get :

a1:b1
a2:b2
a3:b5
1 Answers

You can use

\w+:.*?(?=\s*\w+:|$)

See the regex demo.

Details:

  • \w+ - one or more word chars
  • : - a colon
  • .*? - any zero or more chars other than line break chars, as few as possible
  • (?=\s*\w+:|$) - a positive lookahead that requires zero or more whitespaces, one or more word chars and a colon, or end of string, immediately to the right of the current location.
Related