11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Table 8-6: Instance Properties of RegExp Objects<br />

Property Value Example<br />

multiline Boolean indicating whether<br />

the multiline<br />

flag (m) was set. This<br />

property is ReadOnly.<br />

source <strong>The</strong> string form of the<br />

regular expression. This<br />

property is ReadOnly.<br />

var pattern = /(cat) (dog)/g;<br />

pattern.test("this is a cat dog<br />

and cat dog");<br />

document.writeln(pattern.<br />

multiline);<br />

// prints false<br />

var pattern = /(cat) (dog)/g;<br />

pattern.test("this is a cat dog<br />

and cat dog");<br />

document.writeln(pattern.source);<br />

// prints (cat) (dog)<br />

<strong>The</strong> RegExp class object also has static properties that can be very useful. <strong>The</strong>se properties<br />

are listed in Table 8-7 and come in two forms. <strong>The</strong> alternate form uses a dollar sign and a<br />

special character and may be recognized by those who are already intimately familiar with<br />

regexps. A downside to the alternate form is that it has to be accessed in an associative array<br />

fashion. Note that using this form will probably confuse those readers unfamiliar with languages<br />

like Perl, so it is definitely best to just stay away from it.<br />

Table 8-7: Static Properties of the RegExp Class Object<br />

Property Alternat<br />

e Form<br />

>$1, $2, …,<br />

$9<br />

Value Example<br />

>None >Strings<br />

holding<br />

the text of the<br />

first<br />

nine<br />

parenthesized<br />

subexpressio<br />

ns<br />

of the most<br />

recent match.<br />

>index >None >Holds the<br />

string<br />

index value of<br />

the first<br />

character in<br />

the most<br />

recent pattern<br />

match. This<br />

property is not<br />

part of the<br />

ECMA<br />

standard,<br />

though it<br />

is supported<br />

widely.<br />

<strong>The</strong>refore, it<br />

>var pattern = /(cat) (dog)/g;<br />

pattern.test("this is a cat dog<br />

and cat dog");<br />

document.writeln<br />

("$1="<br />

document.writeln<br />

("$2="<br />

// prints $1= cat $2 = dog<br />

>var pattern = /(cat) (dog)/g;<br />

pattern.test("this is a<br />

cat dog and cat dog");<br />

document.writeln<br />

(RegExp.index);<br />

// prints 10

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!