0

I am returning data from a database that I am attempting to split. I have a string data that consists of a couple of html tags and text. I will appending these lines of text individually so I am attempting to separate each time the </p> tag appears. For example:

data = '<p>Estimated Revenue for Online Stores in 2020 </p><p>The sales of online stores has increased by 2.7% over the past few months.</p> <p>There is an increase of 3.1% of sales compared to the total sales amount of 2019.</p>'

I've attempted this by using data.split('</p>') which does split the text where I am expecting it, but it is not including the closing </p> tag that I am doing the split method on. Is there an alternative method that I can use or alter the existing .split method that will accomplish the same idea but return the closing </p> tag?

Here is a snippet of my code:

data = '<p>Estimated Revenue for Online Stores in 2020 </p><p>The sales of online stores has increased by 2.7% over the past few months.</p> <p>There is an increase of 3.1% of sales compared to the total sales amount of 2019.</p>'

let splitData = data.split('</p>')

console.log(splitData)

I am expecting an outcome like :

  "<p>Estimated Revenue for Online Stores in 2020 </p>",
  "<p>The sales of online stores has increased by 2.7% over the past few months.</p>",
  "<p>There is an increase of 3.1% of sales compared to the total sales amount of 2019.</p>"
stepheniok
  • 395
  • 3
  • 16
  • Might be easier to just parse the html, and then map the elements to your desired text. Otherwise after you split them, you'll have to add the `` back on the end of them all – Taplar Aug 25 '20 at 22:34
  • instead of splitting by , you can look for the location of and split in index+4 – Popeye Aug 25 '20 at 22:36
  • 3
    Apologies if I'm missing something obvious, but why can't you just tack on the `` yourself once you've done the `split()`? In any case, you might consider using a proper HTML parser to ensure you catch any quirky edge cases you may miss with this method. – esqew Aug 25 '20 at 22:36
  • I agree with Taplar's comment about parsing the HTML as that will actually cover all HTML cases (see: [how to parse HTML with regex](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)), however if your text really will just be sibling `p` elements (no nested `p`s), then you can stick with regex and consider using the [lookbehind operator](https://javascript.info/regexp-lookahead-lookbehind) – Henry Woody Aug 25 '20 at 22:38

1 Answers1

0

This should do it:

data = '<p>Estimated Revenue for Online Stores in 2020 </p><p>The sales of online stores has increased by 2.7% over the past few months.</p> <p>There is an increase of 3.1% of sales compared to the total sales amount of 2019.</p>'

let splitData = data.split('</p>').map(e=>e+"</p>")

splitData.pop()

console.log(splitData)
mikehowles
  • 51
  • 3