Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.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/apache-flex/4.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 - Fatal编程技术网

使用Javascript从字符串中提取url

使用Javascript从字符串中提取url,javascript,Javascript,这听起来像是你可以用谷歌搜索的东西,但你已经搜索了好几个小时了 基本上有这个字符串我是从另一个网站ajaxing 'function onclick(event) { toFacebook("http://www.domain.com.au/deal/url-test?2049361208?226781981"); }' 结果是这样的,因为我正在提取onclick 我只想从字符串中提取url 任何帮助都将不胜感激 ----编辑---- 好吧,如果我去这里。。http://regexlib.co

这听起来像是你可以用谷歌搜索的东西,但你已经搜索了好几个小时了

基本上有这个字符串我是从另一个网站ajaxing

'function onclick(event) { toFacebook("http://www.domain.com.au/deal/url-test?2049361208?226781981"); }'
结果是这样的,因为我正在提取onclick

我只想从字符串中提取url

任何帮助都将不胜感激

----编辑----

好吧,如果我去这里。。http://regexlib.com/RESilverlight.aspx regext在线测试仪

运行这个正则表达式

(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?

在我的字符串上,它完美地突出显示了url。。我只需要让它与JS一起运行?

如果它总是与破折号一起运行(我假设您希望破折号之前的一切),您可以使用拆分方法:

var arr = split(val);

您的数据将在arr[0]

中。您可以在javascript中使用这些函数:

function parseUrl1(data) {
var e=/^((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?$/;

if (data.match(e)) {
    return  {url: RegExp['$&'],
            protocol: RegExp.$2,
            host:RegExp.$3,
            path:RegExp.$4,
            file:RegExp.$6,
            hash:RegExp.$7};
}
else {
    return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
}
}

function parseUrl2(data) {
var e=/((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?/;

if (data.match(e)) {
    return  {url: RegExp['$&'],
            protocol: RegExp.$2,
            host:RegExp.$3,
            path:RegExp.$4,
            file:RegExp.$6,
            hash:RegExp.$7};
}
else {
    return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
}
}
参考资料:http://lawrence.ecorp.net/inet/samples/regexp-parse.php

编辑: 下面是另一个将提取url的解决方案:

var function = 'function onclick(event) { toFacebook("http://www.domain.com.au/deal/url-test?2049361208?226781981"); }'
function.match(/(http:[^"]+)/)[0]
旧答案: 使用正则表达式:


就这样结束了

$a.substring($a.indexOf("http:"), $a.indexOf("?"))

正则表达式是我无法理解的。

你的函数。toString().split(“”)[1]

在我看来,字符串是URL…可能是重复的?确切地说,只有当它与正则表达式匹配时才会返回,而不是部分。同样,有些东西是断开的空白对象
 var urlRegex = /(http?:\/\/[^\s]+)/g;

    var testUrl = text.match(urlRegex);

    if (testUrl === null) {

    return "";

      }else {

    var urlImg = testUrl[0];

    return urlImg;

     }
 var urlRegex = /(http?:\/\/[^\s]+)/g;

    var testUrl = text.match(urlRegex);

    if (testUrl === null) {

    return "";

      }else {

    var urlImg = testUrl[0];

    return urlImg;

     }