将所有文本实例转换为链接(除非已包含在< a></a>中) [英] Convert all text instances to links (unless already enclosed in <a></a>)

查看:81
本文介绍了将所有文本实例转换为链接(除非已包含在< a></a>中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想进行一些设置,以查找所选单词的所有实例,并将它们转换为相同页面的链接,但已链接或以其他方式包含在锚标记中的内容除外.

I want to set something up to find all instances of selected words and convert them to same-page links, with the exception of anything which is already a link or otherwise enclosed in an anchor tag.

例如:

makelinkfunction("text_string", "link")

将其转换为

<p>
Here is a text_string to dynamically link.<br/>
This <a href="www.google.com">text_string</a> links elsewhere.
<a name="link">This text_string is within the default link destination.<a/>
</p>

对此:

<p>
Here is a <a href="#link">text_string</a> to dynamically link.<br/>
This <a href="www.google.com">text_string</a> links elsewhere.
<a name="link">This text_string is within the default link destination.<a/>
</p>

第一行的"text_string"实例将转换为链接,而其他两个已经包含在标签中的实例将被保留.

The "text_string" instance on the first line is converted to a link, while the other two, being already enclosed in tags, are left alone.

这不是重复的问题.我不仅在寻找将字符串的所有实例转换为链接的脚本.我已经找到了几种可能的解决方案(我选择使用的一种解决方案是使用Ben Alman的jQuery replaceText插件,因为它用途广泛且易于使用,但我绝不束缚它.)

This is not a duplicate question. I am not just looking for a script to convert all instances of a string into a link. I have found several possible solutions for that (tthe one I've chosen to go with is to use Ben Alman's jQuery replaceText plugin, because it is versatile and easy to use, but I am in no way tied to it.)

正如我所说,我需要能够为已经被标签包围的文本实例添加例外.

I need to be able to add exceptions, as I stated, for instances of text which are already enclosed by tags.

推荐答案

创建jQuery原型函数;

Create jQuery prototype function;

$.fn.makeLink = function(a, b) {

        //Setting function options...
        var options= {

            //Rename function arguments
            insideText: a,
            url: b,

            //Static html
            html: $(this).html(),

            //Already existing anchors
            existsAnchorRGX: new RegExp('<a.*?>.*?' + a + '.*?</a>', 'gi'),
            existsAnchor: [],

            //insadeText RegExp
            insideTextRGX: new RegExp(a, 'gi'),
            insideTextAnchor: '<a href="' + b + '">' + a + '</a>',

            //Temp already anchors
            temp: '<!-- ML:TEMP -->',
            tempRGX: new RegExp('<!-- ML:TEMP -->', 'gi'),

            //Output Html
            outputHtml: $(this).html()

        };

        //loop already anchors and push
        while(alreadyAnchor = options.existsAnchorRGX.exec(options.html))
            options.existsAnchor.push(alreadyAnchor[0]);

        //delete already anchors temporarily
        options.outputHtml = options.outputHtml.replace(options.existsAnchorRGX, options.temp);

        //replace not anchors string
        options.outputHtml = options.outputHtml.replace(options.insideTextRGX, options.insideTextAnchor);

        //replace temp to anchors
        while(options.tempRGX.exec(options.outputHtml)) {
            options.outputHtml = options.outputHtml.replace(options.temp, options.existsAnchor[0]);
            options.existsAnchor.shift();
        }

        //write output html
        $(this).html(options.outputHtml);

        //Reset function options
        option = {};
    };

并使用;

//$('p').makeLink('text_string', 'link');
$('body').makeLink('text_string', 'link');

这篇关于将所有文本实例转换为链接(除非已包含在&lt; a&gt;&lt;/a&gt;中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆