获取以。开头的网址

时间:2017-06-30 06:06:04

标签: javascript regex url extract

我尝试提取以http://damonpyn.com开头的第一个网址。网址格式为http://damonpyn.com/yyyy-mm-ddfb(仅yyyy-mm-dd更改日期)。

以下是我的例子:

var str = '...text before http://damonpyn.com/yyyy-mm-ddfb text after...';
var value = str.match(REGEX???);
output = [{urlpyn: value}];

期望的结果:http://damonpyn.com/yyyy-mm-ddfb

2 个答案:

答案 0 :(得分:1)

var str = '...text before http://damonpyn.com/2017-01-01fb text after...';
var value = str.match(/http:\/\/damonpyn\.com\/\d{4}-\d{2}-\d{2}fb/)[0];
var output = [{urlpyn: value}];

console.log(output);

// Output:
// [ { urlpyn: 'http://damonpyn.com/2017-01-01fb' } ]

答案 1 :(得分:0)

那将是/http:\/\/damonpyn\.com\/\d{4}-\d{2}-\d{2}fb/

日期函数中没有fb,只有大写

var str = '...text before http://damonpyn.com/2015-01-01fb text after...';

console.log(str.match(/http:\/\/damonpyn\.com\/\d{4}-\d{2}-\d{2}fb/));