Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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 使用Jscript进行Url检测_Javascript_Css - Fatal编程技术网

Javascript 使用Jscript进行Url检测

Javascript 使用Jscript进行Url检测,javascript,css,Javascript,Css,这是我在纯文本中为url检测实现的javascrip 但是,当我点击链接时,我无法在“新建”选项卡中打开链接,而是在打开网站之前打开本地主机地址 示例:我检测到了www.google.com 当我点击它时,我得到了这个 (localhost:3415/Home/www.google.com) 我只想在新标签页中打开www.google.com 功能测试(){ dt(document.getElementById('d').value); } 函数dt(t){ //查找www和http.bl

这是我在纯文本中为url检测实现的javascrip 但是,当我点击链接时,我无法在“新建”选项卡中打开链接,而是在打开网站之前打开本地主机地址

示例:我检测到了www.google.com

当我点击它时,我得到了这个

(localhost:3415/Home/www.google.com)
我只想在新标签页中打开www.google.com


功能测试(){
dt(document.getElementById('d').value);
}
函数dt(t){
//查找www和http.bla.bla.TLD的链接(任何查询)
var srg=newregexp(/(^ |\b)([\w.]+\.[a-z]{2,3}(?:\:[0-9]{1,5})(?:\/.*)([,\s]\$)/ig);
var r=t.替换(srg,$1');
document.getElementById('c').innerHTML=r;
}

您需要在开始处添加
http://
。@BenjaminGruenbaum where??我必须补充一点吗?当我输入时,我希望所有文本都是默认的?而且url也有问题,比如www.hacking-ethical.com?你能帮我吗?在jsfiddle.net上创建一个提琴来说明你的问题。通常,您可以在这里完全避免正则表达式,而使用更简单的代码。@BenjaminGruenbaum它通过更改正则表达式来工作:)
<script type="text/javascript">

  function test() {
   dt(document.getElementById('d').value);
  }
  function dt(t) {
   // find links wo www and http. bla.bla.TLD(ANY query)

    var srg = new RegExp(/(^|\b)([\w.]+\.[a-z]{2,3}(?:\:[0-9]{1,5})?(?:\/.*)?)([,\s]|$)/ig);

    var r = t.replace(srg, '$1<a href="$2" target="_blank">$2</a>');

    document.getElementById('c').innerHTML = r;
  }

</script>