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.
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$");
See also: | RegExp pattern - character literal, RegExp() |
ECMA 262 edition 3 - section - 7.8.5
O'Reilly JavaScript Definitive Guide - page - 49
Prev | Home | Next |
Regex | Up | RegExp object |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |