【问题标题】:How to detect Links with out anchor element in a plain text如何检测纯文本中没有锚元素的链接
【发布时间】:2014-07-08 16:27:28
【问题描述】:

如果用户在文本框中输入他的文本并保存它,然后再添加更多文本,他可以编辑该文本并在需要时保存它。

首先,如果用户输入带有一些链接的文本,我会检测到它们并将任何超链接转换为在新选项卡中链接。其次,如果用户想要添加更多文本和链接,他点击编辑并添加它们并保存它此时我必须忽略已经与锚按钮超链接的链接

请帮助和建议

例如:

what = "<span>In the task system, is there a way to automatically have any site / page URL or image URL be hyperlinked in a new window?</span><br><br><span>So If I type or copy http://www.stackoverflow.com/&nbsp; for example anywhere in the description, in any of the internal messages or messages to clients, it automatically is a hyperlink in a new window.</span><br><a href="http://www.stackoverflow.com/">http://www.stackoverflow.com/</a><br>    <br><span>Or if I input an image URL anywhere in support description, internal messages or messages to cleints, it automatically is a hyperlink in a new window:</span><br> <span>https://static.doubleclick.net/viewad/4327673/1-728x90.jpg</span><br><br><a href="https://static.doubleclick.net/viewad/4327673/1-728x90.jpg">https://static.doubleclick.net/viewad/4327673/1-728x90.jpg</a><br><br><br><span>This would save us a lot time in task building, reviewing and creating messages.</span>



Test URL's
        http://www.stackoverflow.com/
        http://stackoverflow.com/
        https://stackoverflow.com/
        www.stackoverflow.com
        //stackoverflow.com/
        <a href='http://stackoverflow.com/'>http://stackoverflow.com/</a>";

我试过这段代码

function Linkify(what) {
    str = what; out = ""; url = ""; i = 0;
    do {
        url = str.match(/((https?:\/\/)?([a-z\-]+\.)*[\-\w]+(\.[a-z]{2,4})+(\/[\w\_\-\?\=\&\.]*)*(?![a-z]))/i);
        if(url!=null) {
            // get href value
            href = url[0];
            if(href.substr(0,7)!="http://") href = "http://"+href;

            // where the match occured
            where = str.indexOf(url[0]);

            // add it to the output
            out += str.substr(0,where);

            // link it
            out += '<a href="'+href+'" target="_blank">'+url[0]+'</a>';

            // prepare str for next round
            str = str.substr((where+url[0].length));
        } else {
            out += str;
            str = "";
        }
    } while(str.length>0);
    return out;
}

请帮忙 谢谢。

【问题讨论】:

  • 能提供一个js小提琴吗?
  • 你为什么不直接使用str.replace(/yourRegExp/, '') ?
  • @Dwza 替换/匹配没关系我怎么能忽略已经用锚按钮超链接的链接
  • 这是我的另一个question 并得到了答案

标签: javascript jquery html


【解决方案1】:

这是一个正则表达式,您可以在其中选择所有链接而无需锚点

(?:(?:http(?:s)?(?:\:\/\/))?(?:www\.)?(?:\w)*(?:\.[a-zA-Z]{2,4}\/?))(?!([\/a-z<\/a>])|(\'|\"))

这是 RegExFiddle (更新时间 14:41)

退出一项艰巨的任务,因为在 javascript 中您没有 preceded by 语句。 :)

EDIT1:现在它检测到...

http://www.abc.xy
http://abc.xy
https://www.abc.xy
https://abc.xy
www.abc.xy
abc.xy

EDIT2:

这里有点短和用法的问题

正则表达式

/((http(s)?(\:\/\/))?(www\.)?(\w)*(\.[a-zA-Z]{2,4}\/?))(?!([\/a-z<\/a>])|(\'|\"))/g

功能

function Linkify(str) {
    var newStr =  str.replace(/((http(s)?(\:\/\/))?(www\.)?(\w)*(\.[a-zA-Z]{2,4}\/?))(?!([\/a-z<\/a>])|(\'|\"))/g,'<a href="$1">$1</a>');
    return newStr;
}

var newData = Linkify(data);

WORKING JS-FIDDLE

编辑 1.000.000 :D

/((http(s)?(\:\/\/))?(www\.)?([\w\-\.\/])*(\.[a-zA-Z]{2,3}\/?))(?!(.*a>)|(\'|\"))/g

这解决了你的问题。

您将在这里遇到的唯一问题是,未选择点后的 4 个字母。例如.info,如果您希望他们被选中,则将{2,3} 更改为{2,4} 但要小心...如果有人添加了my name is.john 之类的文本,而不是将is.john 转换为链接。

编辑 2.0

如果您有一个非常复杂的 URL,如下所示

((http(s)?(\:\/\/))?(www\.)?([\a-zA-Z0-9-_\.\/])*(\.[a-zA-Z]{2,3}\/?))([\a-zA-Z0-9-_\/?=&#])*(?!(.*a>)|(\'|\"))

匹配

https://stackoverflow.com/questions/34170950/summernote-inserthtml?firstname=channaveer&lastname=hakari#fsdfsdf

【讨论】:

  • 你测试给定的正则表达式了吗?
  • @dhee 抱歉,我在工作 :) 更新了正则表达式,现在它还检测到 www。
  • @dhee 如果你接受答案如果这解决了你的问题会很好:)
  • 我仍然面临这个 regx 的一些问题 :(
  • 使用这个 url 进行 Dwza 测试失败 :( static.doubleclick.net/viewad/4327673/1-728x90.jpg 我在 regx 形成或创建方面很差,请你帮忙
【解决方案2】:

一个更简单的解决方案可能是删除您创建的链接(这样用户在再次单击“编辑”时会得到他们键入的内容)。

另一个想法是在&lt;/a&gt; 处拆分字符串。这为您提供了一个字符串列表,这些字符串都以锚元素结尾(最后一个除外)。遍历这个列表,去掉最后一个&lt;a之后的部分,链接化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 2022-06-25
    相关资源
    最近更新 更多