Chapter 7. The String and RegExp Objects

In Chapter 3, we looked at the various data types supported in JavaScript briefly. Now we will look at them in detail, beginning with the String object, which is the structure used to represent all strings in JavaScript. A string is an arbitrary sequence of characters. Program messages, user input, and any other text data are represented as strings. Here we'll cover its built-in features, how they interact with other types — specifically the Number type — and some common ways developers use strings in their programs. Later we'll talk about regular expressions and how they can be used with strings to provide sophisticated and high-speed searching. If you are coming from the Java world, much of this will be familiar. Good portions of the specification for Strings come directly from Java. Also, C# and C++ have similar features as well. However, JavaScript holds plenty of surprises that you may not expect coming from another language, as you will see.

String Object Overview

All strings in JavaScript become instances of the String object, a wrapper class and a member of the global object. This automatically extends certain properties and methods to each string created. It's important to remember, though, that although strings are objects, there is also a string primitive which the String wrapper class enhances with additional functionality. The String wrapper class consists of the following members:

List of Properties


String.length ...

Get JavaScript® Programmer's Reference now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.