[英]JavaScript - splitting a string on specific character
我正在从我试图拆分的数据库中返回数据。 我有一个由几个 html 标签和文本组成的字符串data
。 我将单独附加这些文本行,因此每次出现</p>
标记时我都会尝试将其分开。 例如:
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>'
我已经通过使用data.split('</p>')
尝试了这一点,它确实在我期望的地方拆分了文本,但它不包括我正在执行拆分方法的结束</p>
标记。 是否有替代方法可以使用或更改现有的.split
方法来实现相同的想法但返回结束</p>
标记?
这是我的代码片段:
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)
我期待这样的结果:
"<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>"
这应该这样做:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.