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

0

2 Answers 2

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))

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.