Okay, I've update this question with improved code from the answers and comments below and more similar to the real project. But still it's not working in IE8. fiddle here
好的,我用下面的答案和评论中的改进代码更新了这个问题,更类似于真实的项目。但它仍然无法在IE8中运行。在这里小提琴
<ul id="definitions">
<li id="defid63">keyword1<input type="hidden" value="63" /></li>
<li id="defid61">keyword2<input type="hidden" value="61" /></li>
<li id="defid62">Keyword3<input type="hidden" value="62" /></li>
</ul>
<div id="html">Lorem ipsum keyword1 dolor keyword2 sit keyword3 amet, consectetur adipisicing elit, sed do eiusmod tempor</div>
I've a list (ul > li
) of keywords and there is a string with some text. I would like to wrap each occurrence of a keyword with an <a>
-tag. I've code which works fine in firefox and chrome, but IE(8) doesn't match the regex somehow.
我有一个关键字列表(ul> li),并且有一个包含一些文本的字符串。我想用 -tag包装每个匹配关键字。我的代码在firefox和chrome中运行良好,但IE(8)与正则表达式不一致。
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 defid = $(this).find('input').val(),
deftext = $(this).html().split("<input")[0],
//the keyword
//pattern = eval('/\\b('+RegExp.quote(deftext)+')\\b/gi');
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);
});
3 个解决方案
#1
2
This should work:
这应该工作:
var pattern = new RegExp('\b('+def+')\b','gi');
passing variable to a regexp in javascript
在javascript中将变量传递给regexp
#2
1
This works in IE 8; test it in this fiddle.
这适用于IE 8;在这个小提琴中测试它。
Here's what I did:
这是我做的:
- replaced eval() with RegExp
- 用RegExp替换了eval()
- escaped the reference text before using in regex
- 在使用正则表达式之前转义参考文本
- fudged an id since you didn't provide that code
- 由于你没有提供该代码,因此捏造了一个id
Here is the code
这是代码
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);
});
#3
0
Okay, the actual problem was lying inside the code which I didn't provide in the first version of my question. I used the code deftext = $(this).html().split("<input")[0]
, which doesn't work in IE8 because IE changes tags into uppercase characters. Browsers like FF and Chrome don't do that. So, using eval() to make an expression does work and wasn't really the problem: demo here http://jsfiddle.net/zluiten/79rhW/
好的,实际的问题是在我的问题的第一个版本中没有提供的代码中。我使用代码deftext = $(this).html()。split(“ ”)[0],这在ie8中不起作用,因为ie将标签更改为大写字符。像ff和chrome这样的浏览器不会这样做。因此,使用eval()来创建表达式确实有效,并不是真正的问题:这里演示http:>
#1
2
This should work:
这应该工作:
var pattern = new RegExp('\b('+def+')\b','gi');
passing variable to a regexp in javascript
在javascript中将变量传递给regexp
#2
1
This works in IE 8; test it in this fiddle.
这适用于IE 8;在这个小提琴中测试它。
Here's what I did:
这是我做的:
- replaced eval() with RegExp
- 用RegExp替换了eval()
- escaped the reference text before using in regex
- 在使用正则表达式之前转义参考文本
- fudged an id since you didn't provide that code
- 由于你没有提供该代码,因此捏造了一个id
Here is the code
这是代码
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);
});
#3
0
Okay, the actual problem was lying inside the code which I didn't provide in the first version of my question. I used the code deftext = $(this).html().split("<input")[0]
, which doesn't work in IE8 because IE changes tags into uppercase characters. Browsers like FF and Chrome don't do that. So, using eval() to make an expression does work and wasn't really the problem: demo here http://jsfiddle.net/zluiten/79rhW/
好的,实际的问题是在我的问题的第一个版本中没有提供的代码中。我使用代码deftext = $(this).html()。split(“ ”)[0],这在ie8中不起作用,因为ie将标签更改为大写字符。像ff和chrome这样的浏览器不会这样做。因此,使用eval()来创建表达式确实有效,并不是真正的问题:这里演示http:>