JavaScript: Explode a string

There is a mobile optimized version of this page, view AMP Version.

Those of you who are familiar with PHP know a very useful function called explode() which takes a string and a delimiter and splits the string into an array using the delimiter as the splitter.

So for instance a string “this|is|a|test” can be split up into an array where:

[0] =  this
[1] = is
[2] = a
[3] = test

Well this is also possible in JavaScript using .split(), here is an example of how it works:

var text = "this|is|a|test";
var split = text.split("|");

document.write(split[0] + " " + split[1] + " " + split[2] + " " + split[3]);

Note: The delimiter is removed from the string and represents the “space” between each item that will be in the array.

Author: Dean Williams

I'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips