0

我有以下内容,我正在动态创建一个链接,我被拆分的串联所困扰。

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/)[]

等等。

4

3 回答 3

3

用这个:

link.split(/\d/).slice(1).join('') 
于 2012-12-06T11:52:11.483 回答
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>";
于 2012-12-06T11:51:08.367 回答
1

不需要拆分,只需获取空格后面的字符串部分即可:

link.substr(link.indexOf(" ") + 1);
于 2012-12-06T11:55:40.800 回答