检测文本中的超链接 - jQuery

发布于 2024-12-10 01:00:14 字数 183 浏览 1 评论 0原文

我有一个纯文本,例如,“你好,你好吗,请访问 'http://google.com'”

我使用 jQuery 将其显示在 div 中(该文本是随机生成的)。我的问题是,有什么方法可以检测到文本中的“http://google.com”是超链接,从而将文本的该部分转换为可点击的超链接?

谢谢。

I have a plain text, say, "Hello how are you, please visit 'http://google.com'".

I am displaying this in a div using jQuery (this text is being randomly generated). My question is, is there any way that I can detect that "http://google.com" in the text is a hyperlink and thereby convert that part of the text in to a clickable hyperlink?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风挽心 2024-12-17 01:00:14

如果您使用 jQuery,您应该查看 linkify,它会自动为您完成此操作。

$("#content").linkify();

来源可在此处获取:https://code.google.com/archive/p/jquery -linkify/
在这里: http://www.dave-smith.info/jquery.linkify / (web.archive.org 上的镜像)

If you are using jQuery you should check out linkify, which does it automatically for you.

$("#content").linkify();

source available here: https://code.google.com/archive/p/jquery-linkify/
and here: http://www.dave-smith.info/jquery.linkify/ (mirror at web.archive.org)

你不是我要的菜∠ 2024-12-17 01:00:14

这个正则表达式对我有用(用于匹配 URL 的改进的自由、准确的正则表达式模式的稍微修改版本) 。

text = text.replace(
         /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]
         |[a-z0-9.-]+[.][a-z]{2,4}\/)(?:(?:[^\s()<>.]+[.]?)+|((?:[^\s()<>]+
         |(?:([^\s()<>]+)))))+(?:((?:[^\s()<>]+|(?:([^\s()<>]+))))
         |[^\s`!()[]{};:'".,<>?«»“”‘’]))/gi,
         "<a target=_blank href=$1>$1</a>");

This regex works for me (slightly modified version of An Improved Liberal, Accurate Regex Pattern for Matching URLs).

text = text.replace(
         /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]
         |[a-z0-9.-]+[.][a-z]{2,4}\/)(?:(?:[^\s()<>.]+[.]?)+|((?:[^\s()<>]+
         |(?:([^\s()<>]+)))))+(?:((?:[^\s()<>]+|(?:([^\s()<>]+))))
         |[^\s`!()[]{};:'".,<>?«»“”‘’]))/gi,
         "<a target=_blank href=$1>$1</a>");

成熟稳重的好男人 2024-12-17 01:00:14

我知道已经晚了。我到处寻找答案。你可以试试这个。

var get_words = 'This is a long text. It contains 150 characters. You can find more about this text on this link http://www.somewebsite.com/RDFCCSDVDS';
$('p').html(beautify_text(get_words));

function beautify_text(text){
  var $words = text.split(' ');
  for (i in $words) {
      if ($words[i].indexOf('http://') == 0) {
          $words[i] = '<a href="' + $words[i] + '" target="_blank">' + $words[i] + '</a>';
      }
  }
  return $words.join(' ');
}

I know it's late. I've search anywhere to find an answer. You can try this.

var get_words = 'This is a long text. It contains 150 characters. You can find more about this text on this link http://www.somewebsite.com/RDFCCSDVDS';
$('p').html(beautify_text(get_words));

function beautify_text(text){
  var $words = text.split(' ');
  for (i in $words) {
      if ($words[i].indexOf('http://') == 0) {
          $words[i] = '<a href="' + $words[i] + '" target="_blank">' + $words[i] + '</a>';
      }
  }
  return $words.join(' ');
}
五里雾 2024-12-17 01:00:14

您可以通过执行以下操作来实现此目的:

<?php

// The Regular Expression filter

$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

// The Text you want to filter for urls

$text = "Hello how are you, please visit http://google.com";

// Check if there is a url in the text

if(preg_match($reg_exUrl, $text, $url)) {

       // make the urls hyper links

       echo preg_replace($reg_exUrl, "<a href="{$url[0]}">{$url[0]}</a> ", $text);

} else {

       // if no urls in the text just return the text

       echo $text;

}
?>

这是一个完整的教程:
查找文本中的网址并创建链接

希望这有帮助。

You can achieve this by doing:

<?php

// The Regular Expression filter

$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

// The Text you want to filter for urls

$text = "Hello how are you, please visit http://google.com";

// Check if there is a url in the text

if(preg_match($reg_exUrl, $text, $url)) {

       // make the urls hyper links

       echo preg_replace($reg_exUrl, "<a href="{$url[0]}">{$url[0]}</a> ", $text);

} else {

       // if no urls in the text just return the text

       echo $text;

}
?>

Here is a complete tutorial:
Find URLs in text and make links

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文