Javascript 正则表达式替换不在IE中工作

Javascript 正则表达式替换不在IE中工作,javascript,jquery,regex,internet-explorer,Javascript,Jquery,Regex,Internet Explorer,好的,我已经用下面答案和评论中改进的代码更新了这个问题,并且与实际项目更为相似。但它在IE8中仍然不起作用 这应该起作用: var pattern = new RegExp('\b('+def+')\b','gi'); 这应该可以: var pattern = new RegExp('\b('+def+')\b','gi'); 这在IE 8;中有效 以下是我所做的: 将eval()替换为RegExp 在正则表达式中使用前对引用文本进行转义 伪造身份证,因为你没有提供密码 这是密码 jQu

好的,我已经用下面答案和评论中改进的代码更新了这个问题,并且与实际项目更为相似。但它在IE8中仍然不起作用

这应该起作用:

var pattern = new RegExp('\b('+def+')\b','gi');

这应该可以:

var pattern = new RegExp('\b('+def+')\b','gi');

这在IE 8;中有效

以下是我所做的:

  • 将eval()替换为RegExp
  • 在正则表达式中使用前对引用文本进行转义
  • 伪造身份证,因为你没有提供密码
这是密码

jQuery(function($) {

    // created by Gracenotes
    // http://goo.gl/MTkcY
    RegExp.quote = function(str) {
       return str.replace(/([.?*+^$[\]\\(){}-])/g, "\\$1");
    };

    var $node = $('#html'),   // the dom element
        html  = $node.html(); // the text we'll manipulate

    $('#definitions li').each(function() {
        var $this   = $(this),
            //I didn't know where defid came from
            //so I just added it as id attribute
            defid   = $this.attr('id'), 
            deftext = $(this).text(), //the keyword
            pattern = new RegExp('\\b(' + RegExp.quote(deftext) + ')\\b', 'gi');
        html = html.replace(pattern, '<a href="#id=' + defid + '">$1</a>');
    });

    // replace the dom content with our updated text
    $node.html(html);

});
jQuery(函数($){
//由Gracenotes创建
// http://goo.gl/MTkcY
RegExp.quote=函数(str){
返回str.replace(/([.?*+^$[\]\\(){}-])/g,“\\$1”);
};
var$node=$('#html'),//dom元素
html=$node.html();//我们要处理的文本
$('#定义li')。每个(函数(){
变量$this=$(this),
//我不知道迪菲德是从哪里来的
//所以我只是把它添加为id属性
defid=$this.attr('id'),
deftext=$(this).text(),//关键字
pattern=newregexp('\\b('+RegExp.quote(deftext)+')\\b',gi');
html=html.replace(模式“”);
});
//用更新的文本替换dom内容
$node.html(html);
});
这在IE 8;中有效

以下是我所做的:

  • 将eval()替换为RegExp
  • 在正则表达式中使用前对引用文本进行转义
  • 伪造身份证,因为你没有提供密码
这是密码

jQuery(function($) {

    // created by Gracenotes
    // http://goo.gl/MTkcY
    RegExp.quote = function(str) {
       return str.replace(/([.?*+^$[\]\\(){}-])/g, "\\$1");
    };

    var $node = $('#html'),   // the dom element
        html  = $node.html(); // the text we'll manipulate

    $('#definitions li').each(function() {
        var $this   = $(this),
            //I didn't know where defid came from
            //so I just added it as id attribute
            defid   = $this.attr('id'), 
            deftext = $(this).text(), //the keyword
            pattern = new RegExp('\\b(' + RegExp.quote(deftext) + ')\\b', 'gi');
        html = html.replace(pattern, '<a href="#id=' + defid + '">$1</a>');
    });

    // replace the dom content with our updated text
    $node.html(html);

});
jQuery(函数($){
//由Gracenotes创建
// http://goo.gl/MTkcY
RegExp.quote=函数(str){
返回str.replace(/([.?*+^$[\]\\(){}-])/g,“\\$1”);
};
var$node=$('#html'),//dom元素
html=$node.html();//我们要处理的文本
$('#定义li')。每个(函数(){
变量$this=$(this),
//我不知道迪菲德是从哪里来的
//所以我只是把它添加为id属性
defid=$this.attr('id'),
deftext=$(this).text(),//关键字
pattern=newregexp('\\b('+RegExp.quote(deftext)+')\\b',gi');
html=html.replace(模式“”);
});
//用更新的文本替换dom内容
$node.html(html);
});


好的,实际问题在于我在问题的第一个版本中没有提供的代码中。我使用了代码
deftext=$(this.html().split(好的,实际问题在于我在问题的第一个版本中没有提供的代码内部。我使用了代码
deftext=$(this.html().split)(
var pattern=new RegExp('\\b('+def+')\\b','gi会发生什么?
)使用“eval()”看起来很恶心。你还是会提前避开eval()--不是说特种部队会从你的天花板下来偷走你的键盘或任何东西;但是RegExp是完成这项工作的合适工具:)你完全正确,我不会再这样做;)var pattern=new RegExp('\\b('+def+)\\b,''gi')会发生什么?使用“eval()”似乎很讨厌。你仍然可以提前避免eval()——并不是说特别行动小组会从你的天花板下来偷走你的键盘或其他东西;但是RegExp是完成这项工作的合适工具:)你完全正确,我不会再这样做了;)在对字符串调用
new RegExp
时,您肯定不希望使用斜杠。嗯,我用小提琴\\b对它进行了测试,它就像一个符咒;给出了什么?@Kato如果你是指我的评论,我指的是mblase75的答案最初包含了前斜杠,而对于RegExp文本,我没有解决字符串文本中反斜杠数量的问题:)@Dan aaaaaaaa h:)所以我想知道\b是否与\\b(运行到小提琴)相反,真正的问题在于我在问题的第一个版本中没有提供的代码。我使用了代码
deftext=$(this.html().split)("非常确定在对字符串调用
new RegExp
时您不需要斜杠。嗯,我用小提琴\\b测试了它,它就像一个符咒一样工作;给了什么?@Kato如果您是指我的评论,我指的是mblase75的答案最初包括向前斜杠的事实,而对于RegExp文本,我并没有解决数量的问题字符串文本中的反斜杠:)@Dan aaaaaaaaaah:)因此我想知道\b是否与\\b(运行to fiddle)相反。好的,实际问题在于我在问题的第一个版本中没有提供的代码中。我使用了代码
deftext=$(this.html().split)(“好的,实际问题在于我在问题的第一个版本中没有提供的代码中。我使用了代码
deftext=$(this.html().split(”好的,实际问题在于我在问题的第一个版本中没有提供的代码中。我使用了代码
deftext=$(this.html().split()