移除HTML中重复ID的连续span元素,保留单个并合并文本
问题需求
处理一段HTML字符串时,需要实现以下操作:
- 选中所有
<span>元素 - 移除连续排列的同ID重复
<span>,仅保留一个 - 将所有被移除的同ID
<span>内的文本合并到保留的那个<span>中 - 不得删除
<span>元素之外的其他文本
示例
输入
<p> Hi,<span id="1">this is just a simple text and we</span> <span id="1">want to save me because i had a lot of</span> <span id="1">pressure on and i want to live a better life. 😭</span> I researched a lot about it but i could't find anything helpful <span id="2">just another rant. 😭</span> </p>
输出
<p> Hi, <span id="1"> this is just a simple text and we want to save me because i had a lot of pressure on and i want to live a better life 😭 </span> I researched a lot about it but i could't find anything helpful <span id="2">just another rant. 😭</span> </p>
内容的提问来源于stack exchange,提问作者Samyar
相关产品推荐
相关产品推荐

