串联数组的串联

时间:2012-12-06 11:48:18

标签: javascript regex string split

我有以下内容,我正在动态创建一个链接我遇到了一个分裂的连接。

link = "<li><span id='number'>" + link.split(" ")[0] + ".</span>" // Adding a "peroid" character after the reason number and make it bold 
        + "<a href='#" + reasonTitle + "' " // Open link tag off adding href with relevant reference
        + "onclick=\"_gaq.push([\'_trackEvent\', \'" + experimentConversionReference + "\', \'ReasonClicked\', \'" + reasonTitleSpaces + "\'])\;\">" // Adding event tracking for google
        + link.split(/\d/)[1] // Add back on the back end of the split string
        + "</a>" // Close link tag off
        + "</li>";

更具体地说,line 4上面我想从[1]抓取并打印数组中的所有内容,我该怎样才能这样做?

我不想做的是

link.split(/\d/)[1] + link.split(/\d/)[2]  + link.split(/\d/)[3]  + link.split(/\d/)[]

等等。

3 个答案:

答案 0 :(得分:3)

使用此:

link.split(/\d/).slice(1).join('') 

答案 1 :(得分:1)

拆分并加入,不包括第一项:

var joined  = link.split(/\d/);
joined .shift(); // remove first item
joined .join(''); // join the array

然后使用它:

link = "<li><span id='number'>" + link.split(" ")[0] + ".</span>" // Adding a "peroid" character after the reason number and make it bold 
        + "<a href='#" + reasonTitle + "' " // Open link tag off adding href with relevant reference
        + "onclick=\"_gaq.push([\'_trackEvent\', \'" + experimentConversionReference + "\', \'ReasonClicked\', \'" + reasonTitleSpaces + "\'])\;\">" // Adding event tracking for google
        + joined // Add back on the back end of the split string
        + "</a>" // Close link tag off
        + "</li>";

答案 2 :(得分:1)

您不需要拆分,只需在空格后获取字符串的一部分:

link.substr(link.indexOf(" ") + 1);