如何使用jquery对单词进行不区分大小写的搜索并将其转换为超链接?

如何使用jquery对单词进行不区分大小写的搜索并将其转换为超链接?,jquery,search,urlize,Jquery,Search,Urlize,我想将某些关键字转换为我网站上的附属链接。我不想在每个页面中手工编写每个链接的代码。因此,我正在寻找一种基于Javascript/Jquery的解决方案,其中使用给定的关键字列表和相应的URL(二维数组),在页面加载时,使用相应的URL将这些关键字“URL化” 我发现这个注释包含不区分大小写的搜索代码,但是它将整个文本节点而不仅仅是单词URL化 我可以使用下面的代码。但问题是,它也会将元素中的关键字URL化。我希望它们只在元素中URL化 <script type="text/javascr

我想将某些关键字转换为我网站上的附属链接。我不想在每个页面中手工编写每个链接的代码。因此,我正在寻找一种基于Javascript/Jquery的解决方案,其中使用给定的关键字列表和相应的URL(二维数组),在页面加载时,使用相应的URL将这些关键字“URL化”

我发现这个注释包含不区分大小写的搜索代码,但是它将整个文本节点而不仅仅是单词URL化

我可以使用下面的代码。但问题是,它也会将
元素中的关键字URL化。我希望它们只在
元素中URL化

<script type="text/javascript">
(function($) {
  var thePage = $("body");
  thePage.html(thePage.html().replace(/testing/ig, '<a href="http://testing.com">testing</a>')); 
})(jQuery)
</script>
<p>I'm going to test urlification</p>
<pre>
function test() {
alert(' test ');
}
</pre>

(函数($){
var thePage=$(“主体”);
html(thePage.html().replace(/testing/ig');
})(jQuery)
例如:

<p>I'm going to <a href="test123.com">test</a> urlification</p>
<pre>
function test() {
alert(' test ');
}
</pre>
我要测试urlification

Here is some text with this <span class="URLify">DataItem</span> to be search and replaced with URL or just to be URLified
var foundin = $('p:contains("I am a simple string")');
foundin.each(function () {
    var linktext = $(this).html().replace('I am a simple string','<a href="">I am a simple string</a>');
    $(this).html(linktext);
});  
功能测试(){ 警报(‘测试’); }
应改为

var URL = "http://www.myapp.com/ListOfItems/"; //the url to use or append item to

$('.URLify').wrapInner('<a href="'+URL+'"></a>');
我要去

Here is some text with this <span class="URLify">DataItem</span> to be search and replaced with URL or just to be URLified
var foundin = $('p:contains("I am a simple string")');
foundin.each(function () {
    var linktext = $(this).html().replace('I am a simple string','<a href="">I am a simple string</a>');
    $(this).html(linktext);
});  
功能测试(){ 警报(‘测试’); }
您是否尝试过以下方法:

将它们放在带有类“URLify”的span中,根据您获得的数据,通过使用代码动态创建URL来替换span的内部文本

var foundin = $('p:contains("testing")');
foundin.each(function () {
    var linktext = $(this).html().replace(/testing/ig, '<a href="http://testing.com">testing</a>'));
    $(this).html(linktext);
});  
这里有一些包含此数据项的文本,需要搜索并替换为URL或仅需URL化
在jQuery中,您可以执行类似的操作(未经测试)

var URL=”http://www.myapp.com/ListOfItems/"; //要使用或附加项的url
$('.URLify').wrapInner('');
或者,如果您想将“DataItem”附加到“../ListoItems/”中,您也可以这样做…

您是否尝试过以下方法:

将它们放在带有类“URLify”的span中,根据您获得的数据,通过使用代码动态创建URL来替换span的内部文本

var foundin = $('p:contains("testing")');
foundin.each(function () {
    var linktext = $(this).html().replace(/testing/ig, '<a href="http://testing.com">testing</a>'));
    $(this).html(linktext);
});  
这里有一些包含此数据项的文本,需要搜索并替换为URL或仅需URL化
在jQuery中,您可以执行类似的操作(未经测试)

var URL=”http://www.myapp.com/ListOfItems/"; //要使用或附加项的url
$('.URLify').wrapInner('');
或者,如果您想在“../ListOfItems/”中附加“DataItem”,您也可以这样做…

类似-

// the array of affiliate links
var affiliates = [
        ['google', 'http://www.google.com/'],
        ['bing', 'http://www.bing.com/'],
        ['yahoo', 'http://www.yahoo.com/']
    ],
    $p = $('p'), // the selector to search text within
    i = 0, // index declared here to avoid overhead on the loop
    size = affiliates.length, // size of the affiliates array 
                              // again declared here to avoid overhead
    reg; // the regex holder variable

// loop over all the affiliates array
for(i; i < size; i++){
    // create a regex for each affiliate
    reg = new RegExp('('+affiliates[i][0]+')','gi');

    // finally replace the string with the link
    // NOTE: do not forget to include the aforementioned plugin before this code,
    // otherwise this won't work
    $p.replaceText(reg, '<a href="'+affiliates[i][2]+'">$1</a>');
}
var foundin=$('p:contains(“我是一个简单的字符串”);
每个(函数(){
var linktext=$(this.html().replace('我是一个简单字符串','');
$(this).html(linktext);
});  
或者用你的例子-

var foundin=$('p:contains(“testing”);
每个(函数(){
var linktext=$(this.html().replace(/testing/ig');
$(this).html(linktext);
});  
技术取自-

类似-

// the array of affiliate links
var affiliates = [
        ['google', 'http://www.google.com/'],
        ['bing', 'http://www.bing.com/'],
        ['yahoo', 'http://www.yahoo.com/']
    ],
    $p = $('p'), // the selector to search text within
    i = 0, // index declared here to avoid overhead on the loop
    size = affiliates.length, // size of the affiliates array 
                              // again declared here to avoid overhead
    reg; // the regex holder variable

// loop over all the affiliates array
for(i; i < size; i++){
    // create a regex for each affiliate
    reg = new RegExp('('+affiliates[i][0]+')','gi');

    // finally replace the string with the link
    // NOTE: do not forget to include the aforementioned plugin before this code,
    // otherwise this won't work
    $p.replaceText(reg, '<a href="'+affiliates[i][2]+'">$1</a>');
}
var foundin=$('p:contains(“我是一个简单的字符串”);
每个(函数(){
var linktext=$(this.html().replace('我是一个简单字符串','');
$(this).html(linktext);
});  
或者用你的例子-

var foundin=$('p:contains(“testing”);
每个(函数(){
var linktext=$(this.html().replace(/testing/ig');
$(this).html(linktext);
});  

从-

JQuery中获取的技术不会为您提供文本节点。您必须使用JQuery过滤器显式地检查文本节点,当然,只需遍历DOM即可(在本例中可能更快)。给出了一个实例

JQuery不会给您提供文本节点。您必须使用JQuery过滤器显式地检查文本节点,当然,只需遍历DOM即可(在本例中可能更快)。给出了一个实例

一,。这是你的电话号码
/**
*替换文本字符串的jQuery插件
*
*摘自@linkhttp://net.tutsplus.com/tutorials/javascript-ajax/spotlight-jquery-replacetext/
*/
$.fn.replaceText=函数(仅搜索、替换、文本){
返回此值。每个(函数(){
var node=this.firstChild,
val,new_val,remove=[];
如果(节点){
做{
if(node.nodeType==3){
val=node.nodeValue;
new_val=val.replace(搜索,替换);
如果(新值!==值){
如果(!text_only&&/1)。以下是
/**
*替换文本字符串的jQuery插件
*
*摘自@linkhttp://net.tutsplus.com/tutorials/javascript-ajax/spotlight-jquery-replacetext/
*/
$.fn.replaceText=函数(仅搜索、替换、文本){
返回此值。每个(函数(){
var node=this.firstChild,
val,new_val,remove=[];
如果(节点){
做{
if(node.nodeType==3){
val=node.nodeValue;
new_val=val.replace(搜索,替换);
如果(新值!==值){

如果(!text_only&&/你说的对应URL是什么意思?在代码中,在数组中..?你能提供一个关键字和对应URL的小列表吗?最终结果是什么?你的关键字是在某个标记中进入你的html吗?还是它们进入你的html不知道在哪里?@Fred yes,在数组中的代码中,还是在外部文本中file@Jose Faeti-是的,我不知道这些关键字在文本中的什么位置,它们可能存在,也可能不存在,但是如果它们存在,那么只有
元素中的那些应该被URL化。你说的对应URL是什么意思?在代码中,在数组中..?你能提供一个关键字和对应URL的小列表吗?最终结果会是什么?你的答案是什么在你的html中的某个标记中插入单词?或者它们是在你的html中不知道在哪里?@Fred yes,要么在数组中的代码中,要么在外部文本中file@JoseFaeti-是的,我不知道这些关键字在文本中的什么位置,它们可能存在,也可能不存在,但如果存在,则只有
元素中的那些关键字才应该是URLIFEDI为了将文本包装到span元素中,这些是我正在处理的预生成的静态html文件