0

I have the following, I am creating a link dynamically I am stuck with concatenation of a 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>";

More specifically line 4 on the above I want to grab and print everything in the array from [1] and up how can I do this?

What I don't want is to do

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

and so on.

2
  • You should consider adopting a template framework, e.g. github.com/janl/mustache.js. Commented Dec 6, 2012 at 11:50
  • adding some input en expected output would help understanding the question. Commented Dec 6, 2012 at 11:50

3 Answers 3

3

Use this:

link.split(/\d/).slice(1).join('') 
Sign up to request clarification or add additional context in comments.

2 Comments

This is great however I loose the number on from the second split. 1 My Example 200 String. would return "My Example String" when I want "My Example 200 string"
@Anicho I thought, that you had problems only with spliting and concating, so I have provided the solution for resolving only that :).
1

You don't need to split, just get the part of the string after the space:

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

Comments

1

Split and join, excluding first item:

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

And then use it like:

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>";

1 Comment

nice thanks for the working example went with the .substr example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.