将html标记添加到字符串中的每个句子

时间:2019-04-12 01:52:08

标签: javascript jquery html5

我需要使用jQuery / javascript修改文本的div。 基本上,脚本应查找每个“”。和“!”和“?”并将每个句子包装成dd标签

所以目前我的标记如下:

<div class="test">Some text here. Another text there! And another thing here. Additional stuff? Random text.</div>

最后应该看起来像这样:

<div class="test">
<ul>
   <dd>Some text here</dd> 
   <dd>Another text there</dd>
   <dd>And another thing here</dd>
   <dd>Additional stuff</dd>
   <dd>Random text</dd>
</ul>
</div>

帮助-_-

1 个答案:

答案 0 :(得分:2)

希望这就是您的期望,谢谢

var data = $('.test').text().split(/[.\!?]/) // split string based on '.','!','?'
var str='<ul>'
for(d in data)
 str+='<dd>'+data[d]+'</dd>'
str+='</ul>'
$('.test').html(str)                        //Attach to div test
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="test">Some text here. Another text there! And another thing here. Additional stuff? Random text.</div>