I have this string: title: one description: two
and want to split it into groups like [title: one
, description: two
]
JavaScript
x
2
1
options.match(/(title|description):.+?/gi)
2
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
:
JavaScript
1
4
1
const str = 'title: one description: two';
2
console.log(
3
str.split(/ (?=title|description)/)
4
);