Skip to content
Advertisement

Regex match any characters in string, up until next match

I have this string: title: one description: two

and want to split it into groups like [title: one, description: two]

options.match(/(title|description):.+?/gi)

this was my attempt, but it only captures up to the : and 1 space after, it does not include the text after it, which I want to include all of, up until the second match.

Advertisement

Answer

Split on a lookahead for title or description:

const str = 'title: one description: two';
console.log(
  str.split(/ (?=title|description)/)
);
Advertisement