我想解析一个包含数字的字符串,使表格中的一些单元格的每个数字都可以通过包含该数字的超链接单击。
我有一个字符串,它的格式可以是:12345, 54321, 13542
或12345 and 54321 and 13542
,甚至Number12345
等。单元格中的数字数量没有限制。
我希望这些数字是类似以下内容的超链接:
http://www.example.com/example?q=12345
http://www.example.com/example?q=54321
http://www.example.com/example?q=13542
如果能在JS/JQuery中听到任何可以做到这一点的方法,我将非常感激。
谢谢!
发布于 2013-04-29 13:55:31
只要您的表单元格中只包含文本,这应该会起到作用:
$(document).ready(function(){
$("#myTable td").each(function(){
var text = $(this).html();
text = text.replace(/(\d+)/g, '<a href="http://example.com/$1">$1</a>');
$(this).html(text);
});
});
我在JsFiddle上设置了一个示例来测试它:http://jsfiddle.net/EzfzU/。
发布于 2013-04-29 13:59:08
我将尝试以下函数,它是对先前答案的修改
$(document).ready(function(){
$("#myTable td").each(function(){
var text = $(this).html();
text = text.replace(/(\d+)/gm, '<a href="http://example.com/$1">$1</a>');
$(this).html(text);
});
});
请注意以下差异:
匹配
https://stackoverflow.com/questions/16280230
复制