Book Image

Object-Oriented JavaScript - Second Edition - Second Edition

Book Image

Object-Oriented JavaScript - Second Edition - Second Edition

Overview of this book

JavaScript is the behavior, the third pillar in today's paradigm that looks at web pages as something that consists of clearly distinguishable parts: content (HTML), presentation (CSS) and behavior (JavaScript). Using JavaScript, you can create not only web pages but also desktop widgets, browser and application extensions, and other pieces of software. It's a pretty good deal: you learn one language and then code all kinds of different applications. While there's one chapter specifically dedicated to the web browser environment including DOM, Events and AJAX tutorials, the rest is applicable to the other environments Many web developers have tried coding or adopting some bits of JavaScript, but it is time to "man up" and learn the language properly because it is the language of the browser and is, virtually, everywhere. This book starts from zero, not assuming any prior JavaScript programming knowledge and takes you through all the in-depth and exciting futures hidden behind the facade. Once listed in the "nice to have" sections of job postings, these days the knowledge of JavaScript is a deciding factor when it comes to hiring web developers. After reading this book you'll be prepared to ace your JavaScript job interview and even impress with some bits that the interviewer maybe didn't know. You should read this book if you want to be able to take your JavaScript skills to a new level of sophistication.
Table of Contents (19 chapters)
Object-Oriented JavaScript Second Edition
About the Authors
About the Reviewer
Built-in Functions
Regular Expressions

Appendix D. Regular Expressions

When you use regular expressions (discussed in Chapter 4, Objects), you can match literal strings, for example:

> "some text".match(/me/);

But, the true power of regular expressions comes from matching patterns, not literal strings. The following table describes the different syntax you can use in your patterns, and provides some examples of their use:




Matches a class of characters.

> "some text".match(/[otx]/g);
["o", "t", "x", "t"]


A class of characters defined as a range. For example, [a-d] is the same as [abcd], [a-z] matches all lowercase characters, [a-zA-Z0-9_] matches all characters, numbers, and the underscore character.

> "Some Text".match(/[a-z]/g);
["o", "m", "e", "e", "x", "t"]
> "Some Text".match(/[a-zA-Z]/g);
["S", "o", "m", "e", "T", "e", "x", "t"]


Matches everything that is not matched by the class of characters.

> "Some Text".match(/[^a-z]/g);
["S", " ", "T"]


Matches a or b. The pipe character means OR, and it can be used more than once.

> "Some Text".match(/t|T/g);
["T", "t"]
> "Some Text".match(/t|T|Some/g);
["Some", "T", "t"]


Matches a only if followed by b.

> "Some Text".match(/Some(?=Tex)/g);
> "Some Text".match(/Some(?= Tex)/g);


Matches a only when not followed by b.

> "Some Text".match(/Some(?! Tex)/g);
> "Some Text".match(/Some(?!Tex)/g);


Escape character used to help you match the special characters used in patterns as literals.

> "R2-D2".match(/[2-3]/g);["2", "2"]
> "R2-D2".match(/[2\-3]/g);["2", "-", "2"






New line

Carriage return

Form feed


Vertical tab


White space, or any of the previous five escape sequences.

> "R2\n D2".match(/\s/g);
["\n", " "]


Opposite of the above; matches everything but white space. Same as [^\s]:

> "R2\n D2".match(/\S/g);
["R", "2", "D", "2"]


Any letter, number, or underscore. Same as [A-Za-z0-9_].

> "S0m3 text!".match(/\w/g);
["S", "0", "m", "3", "t", "e", "x", "t"]


Opposite of \w.

> "S0m3 text!".match(/\W/g);
[" ", "!"


Matches a number, same as [0-9].

> "R2-D2 and C-3PO".match(/\d/g);["2", "2", "3"]


Opposite of \d; matches non-numbers, same as [^0-9] or [^\d].

> "R2-D2 and C-3PO".match(/\D/g);
["R", "-", "D", " ", "a", "n", "d", " ", "C", "-", "P", "O"]


Matches a word boundary such as space or punctuation.

Matching R or D followed by 2:

> "R2D2 and C-3PO".match(/[RD]2/g);
["R2", "D2"]

Same as above but only at the end of a word:

> "R2D2 and C-3PO".match(/[RD]2\b/g);

Same pattern but the input has a dash, which is also an end of a word:

> "R2-D2 and C-3PO".match(/[RD]2\b/g);
["R2", "D2"]


The opposite of \b.

> "R2-D2 and C-3PO".match(/[RD]2\B/g);
> "R2D2 and C-3PO".match(/[RD]2\B/g);


Matches the backspace character.


The null character.


Matches a Unicode character, represented by a four-digit hexadecimal number.

> "стоян".match(/\u0441\u0442\u043E/);


Matches a character code represented by a two-digit hexadecimal number.

> "\x64";
> "dude".match(/\x64/g);
["d", "d"]


The beginning of the string to be matched. If you set the m modifier (multi-line), it matches the beginning of each line.

> "regular\nregular\nexpression".match(/r/g);
["r", "r", "r", "r", "r"]
> "regular\nregular\nexpression".match(/^r/g);
> "regular\nregular\nexpression".match(/^r/mg);
["r", "r"]


Matches the end of the input or, when using the multiline modifier, the end of each line.

> "regular\nregular\nexpression".match(/r$/g);
> "regular\nregular\nexpression".match(/r$/mg);
["r", "r"]


Matches any single character except for the new line and the line feed.

> "regular".match(/r./g);
> "regular".match(/r.../g);


Matches the preceding pattern if it occurs zero or more times. For example, /.*/ will match anything including nothing (an empty input).

> "".match(/.*/);
> "anything".match(/.*/);
> "anything".match(/n.*h/);

Keep in mind that the pattern is "greedy", meaning it will match as much as possible:

> "anything within".match(/n.*h/g);
["nything with"]


Matches the preceding pattern if it occurs zero or one times.

> "anything".match(/ny?/g);
["ny", "n"]


Matches the preceding pattern if it occurs at least once (or more times).

> "anything".match(/ny+/g);
> "R2-D2 and C-3PO".match(/[a-z]/gi);
["R", "D", "a", "n", "d", "C", "P", "O"]
> "R2-D2 and C-3PO".match(/[a-z]+/gi);
["R", "D", "and", "C", "PO"]


Matches the preceding pattern if it occurs exactly n times.

> "regular expression".match(/s/g);
["s", "s"]
> "regular expression".match(/s{2}/g);
> "regular expression".match(/\b\w{3}/g);
["reg", "exp"]


Matches the preceding pattern if it occurs between a min and max number of times. You can omit max, which will mean no maximum, but only a minimum. You cannot omit min.

An example where the input is "doodle" with the "o" repeated 10 times:

> "doooooooooodle".match(/o/g);
["o", "o", "o", "o", "o", "o", "o", "o", "o", "o"]
> "doooooooooodle".match(/o/g).length;
> "doooooooooodle".match(/o{2}/g);
["oo", "oo", "oo", "oo", "oo"]
> "doooooooooodle".match(/o{2,}/g);
> "doooooooooodle".match(/o{2,6}/g);
["oooooo", "oooo"]


When the pattern is in parentheses, it is remembered so that it can be used for replacements. These are also known as capturing patterns.

The captured matches are available as $1, $2,... $9

Matching all "r" occurrences and repeating them:

> "regular expression".replace(/(r)/g,  '$1$1');
"rregularr exprression"

Matching "re" and turning it to "er":

> "regular expression".replace(/(r)(e)/g, '$2$1');
"ergular experssion"


Non-capturing pattern, not remembered and not available in $1, $2...

Here's an example of how "re" is matched, but the "r" is not remembered and the second pattern becomes $1:

> "regular expression".replace(/(?:r)(e)/g, '$1$1');
"eegular expeession"

Make sure you pay attention when a special character can have two meanings, as is the case with ^, ?, and \b.