RegExp literal (Definition)

A way of creating and initializing regular expression objects.

Availability:

ECMAScript edition - 3

A RegExp literal is defined as some matching expression enclosed in slash characters.

The grammar for building regular expressions is somewhat complex. The rules are basically straightforward with there being a sequence of individual matching rules assembled together and enclosed in slashes. There can be some modifier flags placed after the second (terminating) slash delimiter.

Here are some example Regular Expression literals:

/^JavaScript/

/19[0-9][0-9]*/

/\binterpreter/i

/squeek/g

You can assign these expressions to variables, which will then contain a RegExp object.

Warnings:

Example code:

   // Declare a variable and assign a RegExp literal to it
   var myPattern = /x$/;
   // Now do the same thing with a RegExp constructor
   var myPattern = new RegExp("x$");

See also:RegExp pattern - character literal, RegExp()

Cross-references:

ECMA 262 edition 3 - section - 7.8.5

O'Reilly JavaScript Definitive Guide - page - 49