Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在文本jQuery中检测超链接_Javascript_Jquery_Replace - Fatal编程技术网

Javascript 在文本jQuery中检测超链接

Javascript 在文本jQuery中检测超链接,javascript,jquery,replace,Javascript,Jquery,Replace,我有一个纯文本,说,“你好,请访问”http://google.com“ 我正在使用jQuery将其显示在一个div中(该文本是随机生成的)。我的问题是,我有没有办法检测到这一点?”http://google.com“文本中有一个超链接,从而将该部分文本转换为可单击的超链接 谢谢 您可以通过以下方式实现: <?php // The Regular Expression filter $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9

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

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


谢谢

您可以通过以下方式实现:

<?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;

}
?>

以下是完整的教程:


希望这对您有所帮助。

如果您正在使用jQuery,您应该查看linkify,它会自动为您执行此操作

$("#content").linkify();
此处提供的来源:
这里:()

这个正则表达式适用于我(略加修改的版本)

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()])))
|[^\s`!()[]{};:“,«»””)/gi,
"");

我知道时间不早了。我已经在任何地方搜索了答案。你可以试试这个

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(' ');
}
var get_words='这是一个长文本。它包含150个字符。您可以在此链接上找到有关此文本的更多信息http://www.somewebsite.com/RDFCCSDVDS';
$('p').html(美化文本(获取单词));
函数美化文本(文本){
var$words=text.split(“”);
for(大写美元){
if($words[i].indexOf('http://')==0){
$words[i]='';
}
}
返回$words.join(“”);
}

问题答案的可能重复实际上是以下主题这是PHP,而不是jQuery。
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(' ');
}