1. Home
  2. Computing & Technology
  3. JavaScript

Regular Expressions

4. Spliting Matches

clr gif

The split method available on string objects can also be used with regular expressions. This allows you to be a bit more flexible in the delimiters that you use within a text string that is typed in when you want to break it up into individual entries in an array. For example:

var myString = 'The boy sat on the log next to the tree.';
var re = / /;
var word = myString.split(re);

In this example, myString is split up into individual words with each word being stored as a separate entry in the word array. Of cous=rse the same effect as with this simple regular expression can be achieved by just testing for a single space using mystring.split(' '); but there is a lot more to regular expressions than we have seen so far.

Up until now we have been looking at how to define regular expressions and use them. In the tutorials that follow we will look at how we can use characters and patterns that have special meanings that will really extend the functionality of these simple tests that we have looked at so far and allow us to achieve some really sophisticated processing in just a few statements.

Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.