-2

I have string in Javascript with same nested tags like:

<Text>
  <Text param={1}>Test Text 1</Text>
  <Text> Test Text 2</Text>
  <Text param={3}> Test Text 3
    <Text>Test Text 4</Text>
    Test Text 5
  </Text>
</Text>

and I need to parse it to array

array[0] = "<Text param={1}>Test Text 1</Text>"
array[1] = "<Text>Test Text 2</Text>"
array[2] = "<Text param={3}>Test Text 3</Text>"
array[3] = "<Text>Test Text 4</Text>"
array[4] = "Test Text 5"

Is there a way to parse it via regex?

Appreciate any help!

Ihor
  • 1
  • 1
    "_Is there a way to parse it via regex?_" - Possibly. Should you? [Definately not](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). – Ivar May 30 '22 at 11:56
  • I agree with @Ivar... you really shouldn't parse html on your own because you'll probably miss some odd scenarios.. you should instead use a DOM parser and for sure never regular expressions. Plus written like that it could be succesfully written like splitting by newline. – Diego De Vita May 30 '22 at 11:58
  • You can get children of root element which is , and call them by using children[0] – Azamat May 30 '22 at 12:04
  • You can use DOMParser but it will add quotes around the attributes: `Test Text 1` – adiga May 30 '22 at 12:08

1 Answers1

0

Do not use regex to parse XML!

Use a DOMParser and work with a structured object, not raw data

let parser = new DOMParser();
let xmlDocument = parser.parseFromString(yourInput, "text/xml");

xmlDocument.getElementsByTagName("Text");
Dropout
  • 13,107
  • 9
  • 51
  • 105