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

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

问题描述

长话短说,我有一个在 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 = /((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]+(|$))/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);

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

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