通过比较三个网址生成网址列表

时间:2016-09-09 22:00:17

标签: javascript node.js

我试图通过比较三个网址来生成20个网址的列表。

示例 - 1:如果我有三个网址:

http://www.test.com/asdf/1/lkjh
http://www.test.com/asdf/2/lkjh
http://www.test.com/asdf/3/lkjh

我想找到这三个链接之间的区别,找到区分数字的位置并生成许多链接,如

http://www.test.com/asdf/1/lkjh
http://www.test.com/asdf/2/lkjh
http://www.test.com/asdf/3/lkjh
http://www.test.com/asdf/4/lkjh
http://www.test.com/asdf/5/lkjh
http://www.test.com/asdf/6/lkjh
http://www.test.com/asdf/7/lkjh
http://www.test.com/asdf/8/lkjh
http://www.test.com/asdf/9/lkjh
http://www.test.com/asdf/10/lkjh
http://www.test.com/asdf/11/lkjh
http://www.test.com/asdf/12/lkjh
http://www.test.com/asdf/13/lkjh
http://www.test.com/asdf/14/lkjh
http://www.test.com/asdf/15/lkjh

示例 - 2:如果我有三个网址:

http://www.test.com/asdf?qwer=1
http://www.test.com/asdf?qwer=2
http://www.test.com/asdf?qwer=3

我想找到这三个链接之间的区别,找到区分数字的位置并生成许多链接,如

http://www.test.com/asdf?qwer=1
http://www.test.com/asdf?qwer=2
http://www.test.com/asdf?qwer=3
http://www.test.com/asdf?qwer=4
http://www.test.com/asdf?qwer=5
http://www.test.com/asdf?qwer=6
http://www.test.com/asdf?qwer=7
http://www.test.com/asdf?qwer=8
http://www.test.com/asdf?qwer=9
http://www.test.com/asdf?qwer=10
http://www.test.com/asdf?qwer=11
http://www.test.com/asdf?qwer=12
http://www.test.com/asdf?qwer=13
http://www.test.com/asdf?qwer=14
http://www.test.com/asdf?qwer=15

请让我知道如何在javascript中执行此操作。

1 个答案:

答案 0 :(得分:1)

如果链接只有一个数字,而您只关心添加1(如示例中所示),则只需要一个链接并可以使用类似的内容,其中 base 是起始URL和 n 是要创建的数字。

function generateLinks(base, n) {
  var result = [];
  result.push(base);
  for (i = 1; i < n; i++) {
    var newLink = base.replace(/\d+/, function(digit) { return Number(digit)+1 });
    result.push(newLink);
    base = newLink;
  }
  return result;
}