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
  • 6,209
  • 8
  • 41
  • 75

2 Answers2

12

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
  • 11,595
  • 2
  • 17
  • 39
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
  • 128
  • 1
  • 8