<p><span><a href="RelpyList.aspx?rid=70864" target="_blank">RelpyList1314:</a></span> This RelpyList is 嘿嘿</p>如上一段HTML代码,我想将“This RelpyList is 嘿嘿”中的 "RelpyList"字符 替换成 "RL"字符,但是a标签中"RelpyList.aspx"与“RelpyList1314”这两字符不要替换。该如何写呢?

解决方案 »

  1.   


    var str='<p><span><a href="RelpyList.aspx?rid=70864" target="_blank">RelpyList1314:</a></span> This RelpyList is 嘿嘿</p>'
    alert(str.replace(/\sRelpyList\s/g," RL "))
      

  2.   

    var s='<p><span><a href="RelpyList.aspx?rid=70864" target="_blank">RelpyList1314:</a></span> This RelpyList is 嘿嘿</p>';
    var reg = /This\s*(RelpyList){1}/g;alert( s.replace(reg, function( $0, $1 ) {
    return $0.replace( $1, "RL");
    }));
      

  3.   

    var s='<p><span><a href="RelpyList.aspx?rid=70864" target="_blank">RelpyList1314:</a></span> This RelpyList is 嘿嘿</p>';
    var reg = /This\s*(RelpyList)/g;alert( s.replace(reg, function( $0, $1 ) {
    return $0.replace( $1, "RL");
    }));
      

  4.   

    以上内容多少存在一定的局限。 var re = new RegExp("<p><span><a [^>]+>[^<]+:</a></span>([^<]+?)</p>", "ig")

    var te=c.replace(re,function($0,$1){
    var r =new RegExp("RelpyList", "ig")
    return $1.replace(r,'<a href="SearchResult.aspx?kw=' + encodeURIComponent(kv) + '" target="_blank">'+kv+'</a>') })

    return $0.replace( $1, no);
    })