Jquery:如何在文本字符串中查找单个数字集并向其添加href标记

Jquery:如何在文本字符串中查找单个数字集并向其添加href标记,jquery,string,text,replace,href,Jquery,String,Text,Replace,Href,我有几个带数字的文本字符串,用于连接到索引中的其他条目(使用哈希,因为它们将一起出现在页面上)。例如: <div class="description"><span>This is some text, which we will reference in entry (330) and also in entry (249).</span></div> 预期结果: <div class="descriptio

我有几个带数字的文本字符串,用于连接到索引中的其他条目(使用哈希,因为它们将一起出现在页面上)。例如:

<div class="description"><span>This is some text, which we will reference in entry (330) and also in entry (249).</span></div>
预期结果:

<div class="description"><span>This is some text, which we will reference in entry (<a href="myindex.html#330">330</a>) and also in entry (<a href="myindex.html#249">249</a>).</span></div>
这是一些文本,我们将在entry()和entry()中引用它们。

谢谢您的帮助。

如评论中所述,您的选择器中缺少一个
,但您可以尝试以下操作:

$.each(numbers, function(n, t) {
  $(".description span").html($(".description span").html().replace(t, "<a href='#" + t + "'>" + t + "</a>"))
})
$。每个(数字,函数(n,t){
$(“.description span”).html($(.description span”).html().replace(t,”))
})
演示

var val=$(“.description span”).html();
变量编号=val.match(/\d+/g);
如果(数字!=null){
$。每个(数字、函数(n、t){
$(“.description span”).html($(.description span”).html().replace(t,”))
})
}


这是一些文本,我们将在条目(330)和条目(249)中引用。
如注释中所述,您的选择器中缺少一个
,除此之外,您可以尝试以下操作:

$.each(numbers, function(n, t) {
  $(".description span").html($(".description span").html().replace(t, "<a href='#" + t + "'>" + t + "</a>"))
})
$。每个(数字,函数(n,t){
$(“.description span”).html($(.description span”).html().replace(t,”))
})
演示

var val=$(“.description span”).html();
变量编号=val.match(/\d+/g);
如果(数字!=null){
$。每个(数字、函数(n、t){
$(“.description span”).html($(.description span”).html().replace(t,”))
})
}


这是一些文本,我们将在条目(330)和条目(249)中引用。
您在
$(“说明span”)
中缺少一个
$(“说明span”)
中缺少一个快速的答案。非常感谢你!如果你不介意我问,只是为了让我了解你的代码是如何工作的,如果n没有使用,函数(n,t)的用途是什么。@Niwa
n
表示索引并且没有使用,但是我们使用t表示对象,在本例中,330和249是一个快速、准确的答案。非常感谢你!如果您不介意我这样问,只是为了让我了解您的代码是如何工作的,那么如果n未使用,函数(n,t)的用途是什么。@Niwa
n
表示索引且未使用,但我们使用t表示对象,在本例中为330和249