RegExp for finding tags and text around/in between them?

I have a text that is a mix of “tags” defined as (<[^<>]+>) and “non-tags”, I’d like to separate them into an array. This is for colorizing text in a small code editor while someone is typing.

Here are some examples of input and what I’d like to have as the output:

"text1<tag1>text2<tag2>text3" -> ["text1", "<tag1>", "text2", "<tag2>", "text3"]

"<tag1>text text<tag2><tag3>" -> ["<tag1>", "text text", "<tag2>", "<tag3>"]

"<tag1><tag2>" -> ["<tag1>", "<tag2>"]

"text text" -> ["text text"]

"<<<tag1>text>><<<tag2>" -> ["<<", "<tag1>", "text>><<", "<tag2>"]

I assume that is something RegExp can do?

Thank you!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.