0

I am having a function takes a string input. the string input is noting but a group of html tags like below

<section><div><span><bold></bold></span></div></section>

and i want the output to be like below

["<section>","<div>","<span>","<bold>","</bold>","</span>","</div>","</section>"]

guys pls help me how to split the html string to an array

yasarui
  • 5,635
  • 7
  • 34
  • 67

2 Answers2

11

You can use regex and .match() to do this.

Demo:

var text = "<section><div><span><bold></bold></span></div></section>";
console.log(text.match(/\<.*?\>/g))
Cuong Le Ngoc
  • 10,875
  • 2
  • 13
  • 37
5

The following approach will also work,

let str = "<section><div><span><bold></bold></span></div></section>";
let newstr = str.replace(/</gi, "<><");
let res = newstr.split("<>").filter(v => v != "");
console.log(res)
trance
  • 118
  • 1
  • 8