RegExp literal (Definition)

A way of creating and initializing regular expression objects.


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:





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


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()


ECMA 262 edition 3 - section - 7.8.5

O'Reilly JavaScript Definitive Guide - page - 49