-2

I have a string:

<p>text1</p>
<p>text2 <span class="theClass">text3</span></p>

What I need to achieve is to split this string into an array containing html tags and pure text. something like:

var array= ["<p>", "text1", "</p>", ...];

I've tried to use regexp: /(?=<)|(?<=>)/g, but javascript does not allow me to use lookbehind expression. Any ideas on how to do this?

bigcat
  • 152
  • 2
  • 11

1 Answers1

2

This should work for you:

const data = `<p>text1</p>
<p>text2 <span class="theClass">text3</span></p>`

const split = data => data.split(/(<.*?>)/g).filter(x => x.trim())

console.log(split(data))
Maciej Kozieja
  • 1,812
  • 1
  • 13
  • 32