(Javascript)将纯文本链接转换为可点击的链接 [英] (Javascript) Convert plain text links to clickable links

查看:134
本文介绍了(Javascript)将纯文本链接转换为可点击的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说,我有一个用Wix.com编辑器创建的网站,几个月前就可以进行编码. 我已经设置了一个自定义评论框,以便用户可以发布他们的评论并阅读其他人的评论.

Long story short, I have a website made under Wix.com editor, and coding was made possible a few months ago. I have set up a custom comment box, so users can post their comments, and read others'.

现在的事情是,注释输入"采用纯文本,并且每当发布链接时,它就会以纯文本显示,没有颜色,没有可点击性.

Now the thing is, the "comment Input" takes plain text, and whenever a link is posted, it is displayed as plain text, no color, no clickability.

我想要一个读取"注释列表并转换以"https"或"http"或"www"开头的所有文本的代码...橙色且可点击(在新标签页中打开)

I want a code that 'reads' the list of comments, and convert every text that begins with 'https' or 'http' or 'www' ... orange and clickable (opening in a new tab)

请问有什么解决方法吗?

Any solution please ?

谢谢!

我尝试了很多事情,例如:

I have tried many things such as :

$w('#text95').html = 
       (/((http:|https:)[^\s]+[\w])/g, '<a href="$1" target="_blank">$1</a>').replace;

text95 =显示的注释(它是一个文本,它会重复显示尽可能多的注释)

text95 = the displayed comments (it is a text that repeats itself for as many comments as there are)

推荐答案

您似乎要替换的语法是错误的.

It looks like you're replace syntax is wrong.

尝试类似的方法,我很确定这会起作用.

Try something like this, I'm pretty sure this will work.

function linkify(inputText) {
    var replacedText, replacePattern1, replacePattern2, replacePattern3;

    //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

    //Change email addresses to mailto:: links.
    replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
    replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

    return replacedText;
}


致电:


Calling it with:

$w('#text95').innerHTML = linkify($w('#text95').html);

这篇关于(Javascript)将纯文本链接转换为可点击的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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