我想在段落的所有单词中添加artoon标签

时间:2019-11-19 12:34:55

标签: javascript jquery html

大家好,我有一段

<div class="test">
 <div>
    <p>
        <strong class="bold">
   Here's how you can wrap <b>these</b> three <p>words</p> in a string. Huzzah!! 
       </strong>
   </p>
 </div>
</div>

如果我要在所有单词中添加标签,而不是预期的输出,应该看起来像这样

<div class="test">
 <div>
    <p>
        <strong class="bold">
  <artoon>Here's</artoon> <artoon>how</artoon> <artoon>you</artoon> <artoon>can</artoon> <artoon>wrap</artoon> <b><artoon>these</artoon></b> <artoon>three</artoon> <p><artoon>words</artoon></p> <artoon>in</artoon> <artoon>a</artoon> <artoon>string.</artoon> <artoon>Huzzah!!</artoon>
     </strong>
 </p>

     我从一天开始就把这个东西弄乱了,所以如果您有解决方法,请在这篇文章中添加评论,我想解决它谢谢您

所以我希望你能理解,如果不能,请添加评论并询问我。

谢谢

I tried this one

1 个答案:

答案 0 :(得分:0)

var txt = document.querySelectorAll('.test')[0].innerHTML;

const re = /<artoon>(<\s*[^>]*>)(.*?)(<\s*[^>]*><\/artoon>)/gi;
const re2 = /([^\s]+)\s/gi;
const result = txt.replace(re2, '<artoon>$1</artoon> ').replace(re, '$1<artoon>$2</artoon>$3')
console.log(result)
<div class="test">
   Here's how you can wrap <b>these</b> three <p>words</p> in a string. Huzzah!!
</div>