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.
You cannot define a RegExp literal that is empty simply with a pair of slash characters. This would define a single line comment delimiter instead.
You must specify an empty RegExp literal like this:
// 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$");
ECMA 262 edition 3 - section - 7.8.5