2013-08-01 47 views
1

我正在做一个REGEXP工具,它接受文本输入并在div中显示结果。 我用<b>取代比赛匹配</b >当显示结果时。 如果它匹配原始文本,它工作正常,但问题是当插入HTML代码替换javascript中的标签正则表达式

我想用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; 
    } 
} 

回答

0

也许你的代码应该看起来更像是:

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/

它不适用于自闭标签。