Take the 2-minute tour ×

I need to break apart a string that always looks like this:

something -- something_else.

I need to put "something_else" in another input field. Currently, this string example is being added to an HTML table row on the fly like this:


I figure "split" is the way to go, but there is very little documentation that I can find.

share|improve this question
So what exactly should be put/appended in/to which element? –  Felix Kling Mar 31 '10 at 19:18
Just curious, what did you search for that you didn't find any documentation? I searched on Google for both "javascript split" and "jquery split" and the first result in both cases was the location I linked to. –  Charles Boyung Mar 31 '10 at 19:23
And I am sorry but I cannot see how your code example is related to your split() problem. Give us more information :) –  Felix Kling Mar 31 '10 at 19:26
I was mistakenly thinking it was a jQuery solution when in fact it's actually a javascript thing. I also saw that documentation but dismissed it too quickly –  Matt Mar 31 '10 at 19:30
You need to remember that jQuery IS javascript - unless you are doing something with selectors (and a few other things that start with $.) you are just doing javascript, not jQuery. –  Charles Boyung Mar 31 '10 at 19:35
show 1 more comment

4 Answers

up vote 228 down vote accepted

Documentation can be found e.g. at MDN. Note that .split() is not a jQuery method, but a native string method.

If you use .split() on a string, then you get an array back with the substrings:

var str = 'something -- something_else';
var substr = str.split(' -- ');
// substr[0] contains "something"
// substr[1] contains "something_else"

If this value is in some field you could also do:

tRow.append($('<td>').text($('[id$=txtEntry2]').val().split(' -- ')[0])));

share|improve this answer
add comment

If it is the basic JavaScript split function, look at documentation, JavaScript split() Method (W3Schools).

Basically, you just do this:

var array = myString.split(' -- ')

Then your two values are stored in the array - you can get the values like this:

var firstValue = array[0];
var secondValue = array[1];
share|improve this answer
+1, however... I realize that JS type names are case-sensitive but still, could you please change the name of the array variable of Array type to something else, not to confuse JS newbs? Cheers! –  TildalWave Jan 28 '13 at 22:15
add comment

Look in JavaScript split() Method


"something -- something_else".split(" -- ") 
share|improve this answer
add comment
    var str;
    var sid = new Array();
    str = "your_string";
    sid = (str.split("_"));


share|improve this answer
var sid = new Array() is useless, it just adds to amount of time the code needs to run. –  epascarello May 11 '12 at 14:39
ok,what's the best of new array()? –  Dev.H May 12 '12 at 13:23
You are treating JavaScript like it is Java or C#. JavaScript does not need typed variables and the split turns it into an Array. –  epascarello May 12 '12 at 16:12
i think when i tring to do that did'nt work with me ,but i will try again ,thanks –  Dev.H May 13 '12 at 11:22
add comment

Your Answer


