簡體   English   中英

替換javascript正則表達式中的標簽

[英]replace tags in javascript regexp

我正在做一個REGEXP工具,該工具接受文本輸入並在div中顯示結果。 顯示結果時,我用<b>匹配</ b>替換匹配項。 如果它與原始文本匹配,則可以正常工作,但問題是在插入HTML代碼時

我想用HTML實體替換除<b>標記之外的所有標記,如何實現?

 var regExpTool = {
    makeRegexp: function(pattern, opts){
        var regexp = new RegExp(pattern, opts);
        return regexp;
    },
    createResults: function(pattern, string, opts){
        var res = string.replace(this.makeRegexp(pattern, opts), "<b>$&</b>")

        results.style.display = "block";
        text.style.display = "none";
        results.innerHTML = res;
  }
}

也許您的代碼應該更像:

var win = window, doc = document, bod = doc.getElementsByTagName('body')[0];
function E(e){
  return doc.getElementById(e);
}
function regExpTool(text, outputElement){
  this.makeBold = function(options){
    outputElement.innerHTML = text.replace(new RegExp('^(<.+>)*('+text+')(<\/.+>)*$', options), '<b>$2</b>')
    outputElement.style.display = 'block';
   };
}

new regExpTool('<div>now</div>', E('wow')).makeBold();

請注意,沒有makeRegexp方法。

這是http://jsfiddle.net/PHPglue/FjEdg/2/

它不能與自閉標簽一起使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM