search

Home  >  Q&A  >  body text

javascript - Regular expression matching, replacing the content in html tags

Regular expression matching, replace the content in the html tag, excluding the content of the html tag itself;
For example:
var str="<p class='article'><p class=' article-item'>article-item</p></p>";

Matching keywords: article;

The desired matching result is:<p class="article"><p class="article-item"><span style="color:red">article</span>- item</p></p>>

某草草某草草2707 days ago831

reply all(1)I'll reply

  • phpcn_u1582

    phpcn_u15822017-06-30 10:01:42

    var a="<p class='article'><p class='article-item'>article-item</p></p>".replace(/>([^<]*)<\//,function($0,$1){
        var a=$1.split('-');
        if(a.length>1){
            return `><span style='color:red'>${a[0]}</span>-${a[1]}<`
        }else{
            return `>${a[0]}<`
        }
    })
    console.log(a)

    reply
    0
  • Cancelreply