1. Technology

JavaScript Split

Dividing up Strings into Arrays Using Regular Expressions

By

In JavaScript the split method works with strings and regular expressions in order to divide up the string into several parts which are then returned to the code as an array. To be able to properly understand how split works in JavaScript we need to therefore understand what all these three object types are.


var myarray = mystring.split(myregexp);

Let's start by considering what a string is in JavaScript. A string is exactly what you would expect it to be - a series of characters that are related together and have some sort of meaning. The most likely content for a string are words or sentences or even paragraphs of text but the string might also contain mathematical formulae or in fact anything else that can be written using a series of characters.

We can create a string in one of two ways.


var mystring = "Here is some text in a string.";
var mystring2 = new String("here is some more text");

You will seldom see the second of these used since the first is shorter and produces the same result. All of the methods belonging to the string object can be used with strings regardless of which of the two ways you create them including the split method which we use to split the string up into its component parts (using whatever criteria we want to use to define how those parts are to be determined).

The second obect that is used with the javaScript split method is the array that is created as a result of running the split. An array in JavaScript is basically a collection of objects. In the case of a JavaScript split the objects to be stored in the array that is returned will all be strings each being a part of the original string.

If we were going to create an array without using a JavaScript split to generate it then there are two ways we can do it.


var myarray = ["Here","is","some","text","in","a","string."];
var myarray2 = new Array("Here","is","some","text","in","a","string.");

The third and most complex object involved in doing a JavaScript split is a regular expression. Basically a regular expression is a way that JavaScript and other languages provide to supply a pattern that can be used to match against text in order to do something with that text. In the case of a JavaScript split the pattern is providing the criteria for how the string is to be split into its component parts. Foe example if our string is expected to contain a sentence such as the one used in our above examples for defining a string then we may want to define a pattern that extracts the words from the string and returns them as an array like the one we have in our above array example. We could just specify that the string is to be broken into pieces everywhere that a space appears in the string by using a string in place of the regular expression giving us split(" ") but that has several disadvantages that using a regular expression helps us to overcome. The words may not be divided up just with single spaces. There may be places where there are multiple spaces between words (which would result in the split returning empty strings for the non-existant words between those spaces) or where words are separated by other whitespace characters. We can therefore get the split to work much better if we use a regular expression that will match to any whitespace between words rather than just to single characters.

As with strings and arrays there are two ways to define regular expressions.


var myregexp = /\s/;
var myregexp2 = new RegExp("\s");

In this particular example the \s is a regular expression pattern to match whitespace.

By setting up an appropriate regular expression to define exactly what we want to match in order to split our string into pieces to stor in our array we can get whatever parts of the string we want in an easy to use form.

  1. About.com
  2. Technology
  3. JavaScript
  4. Javascript Tutorials
  5. Coding Tips
  6. JavaScript Split

©2014 About.com. All rights reserved.