Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.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从文本中仅提取第一个url_Javascript_Jquery - Fatal编程技术网

如何使用javascript从文本中仅提取第一个url

如何使用javascript从文本中仅提取第一个url,javascript,jquery,Javascript,Jquery,我有这个regexp function isValidURL(text) { var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; ...... } 文本是:请使用或如果没有发现,请在 我只需要从下面的文本中提取第一个url。(http://google.com) 返回为url您的意思是这样的: va

我有这个regexp

function isValidURL(text) {

        var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;

        ......

    }
文本是:请使用或如果没有发现,请在

我只需要从下面的文本中提取第一个url。(http://google.com)


返回为url您的意思是这样的:

var text = "Please visit http://stackoverflow.com and ask your question, or use http://google.com";

var match = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/​​​​​​​​​​​​.exec(text)​​​​​​​;

​console.log(match[0]);​
演示:

我想你可以使用这个(只要你的文本中除了url本身没有:/):-


关闭:您最好使用文本
请使用http://google.com 或者如果什么也没找到,就在上面问你的问题http://stackoverflow.com
:)如果url仅为www.stackoverflow.com(无http://…),或者即使url看起来像stackoverflow.com,又如何
var text = "Please visit http://stackoverflow.com and ask your question, or use http://google.com";

var text_split = text.split('://'); 
var text_req = text_split[1].(" "); // splitting with blank space 

var finalURL = "http://" + test_req[0];