1.10 JavaScript
JavaScript introduced Perl-like regular expression support with Version 1.2. This reference covers Version 1.5 as defined by the ECMA standard. Supporting
1.10.1 Supported Metacharacters
JavaScript supports the metacharacters and
Table 1-41. Character representations
Table 1-42. Character classes and class-like constructs
Table 1-43. Anchors and other zero-width tests
Table 1-44. Mode modifiers
Table 1-45. Grouping, capturing, conditional, and control
1.10.2
|
| String |
Strings support four convenience methods for pattern matching. Each method takes a pattern argument, which may be either a RegExp object or a string containing a regular expression pattern.
Match pattern against the string returning either the character position of the start of the first matching substring or -1 .
The
replace( )
method searches the string for a match of
pattern
and
Match pattern against the string returning either an array or -1 . Element 0 of the array contains the full match. Additional elements contain submatches from capture groups. In global ( g ) mode, the array contains all matches of pattern with no capture group submatches.
Return an array of strings broken around pattern . If limit , the array contains at most the first limit substrings broken around pattern . If pattern contains capture groups, captured substrings are returned as elements after each split substring.
| RegExp |
Models a regular expression and contains methods for pattern matching.
RegExp objects can be created with either the RegExp( ) constructor or a special literal syntax /.../ . The parameter pattern is a required regular expression pattern, and the parameter attributes is an optional string containing any of the mode modifiers g , i , or m . The parameter pattern can also be a RegExp object, but the attributes parameter then becomes required.
The constructor can throw two expceptions.
SyntaxError
is thrown if
pattern
is
Boolean, if RegExp has g attribute.
Boolean, if RegExp has i attribute.
The character position of the last match.
Boolean, if RegExp has m attribute.
The text pattern used to create this object.
Search
text
and return an array of strings if the search succeeds and
null
if it fails. Element 0 of the array contains the substring matched by the entire regular expression. Additional elements
If the global flag ( g ) is set, then lastIndex is set to the character position after the match or zero if there was no match. Successive exec( ) or test( ) calls will start at lastIndex . Note that lastIndex is a property of the regular expression, not the string being searched. You must reset lastIndex manually if you are using a RegExp object in global mode to search multiple strings.
Return true if the RegExp object matches text . The test( ) method behaves in the same way as exec( ) when used in global mode: successive calls start at lastIndex even if used on different strings.
//Match Spider-Man, Spiderman, SPIDER-MAN, etc.
var dailybugle = "Spider-Man Menaces City!";
//regex must match entire string
var regex = /spider[- ]?man/i;
if (dailybugle.search(regex)) {
//do something
}
//Match dates formatted like MM/DD/YYYY, MM-DD-YY,...
var date = "12/30/1969";
var p =
new RegExp("(\d\d)[-/](\d\d)[-/](\d\d(?:\d\d)?)");
var result = p.exec(date);
if (result != null) {
var month = result[1];
var day = result[2];
var year = result[3];
//Convert <br> to <br /> for XHTML compliance
String text = "Hello world. <br>";
var pattern = /<br>/ig;
test.replace(pattern, "<br />");
//urlify - turn URL's into HTML links
var text = "Check the website, http://www.oreilly.com/catalog/repr.";
var regex =
"\b" // start at word boundary
+ "(" // capture to
+ "(https?telnetgopherfilewaisftp) :"
// resource and colon
+ "[\w/\#~:.?+=&%@!\-]+?" // one or more valid chars
// take little as possible
+ ")"
+ "(?=" // lookahead
+ "[.:?\-]*" // for possible punct
+ "(?:[^\w/\#~:.?+=&%@!\-]"// invalid character
+ "$)" // or end of string
+ ")";
text.replace(regex, "<a href=\"\"></a>");
JavaScript: The Definitive Guide , by David Flanagan (O'Reilly), is a reference for all JavaScript, including regular expressions.