The RegExp Object


The RegExp Object

Besides regular expression objects, there is also a global object named RegExp that holds data about the most recent regular expression operation. This object always existsdon't confuse it with regular expression objects of the kind just covered. The global RegExp object holds data about the most recent regular expression operation, whereas regular expression objects hold data about their particular regular expression operations. You can find the properties of the RegExp object in Table 20.4.

Table 20.4. The Properties of the RegExp Object

Property

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

index

           

x

x

x

x

 

Read-only

 

This property holds the character position where the first successful match began in the string you're searching.

input

   

x

x

       

x

x

 

Read/write

 

This property holds the string on which a regular expression search was performed. There's a shortcut for this property, following the Perl language: $_ . (That is, you can reach this property's value as RegExp.input or RegExp.$_ .)

lastIndex

           

x

x

x

x

 

Read-only

 

Returns the character position where the next match begins in a searched string.

lastMatch

   

x

x

       

x

x

 

Read-only

 

This property holds the last matched characters from any regular expression search.

lastParen

   

x

x

       

x

x

 

Read-only

 

This property holds the last parenthesized submatch from any regular expression search.

leftContext

   

x

x

       

x

x

 

Read-only

 

This property holds the characters from the beginning of the string up to the position before the beginning of the last match.

rightContext

   

x

x

       

x

x

 

Read-only

 

This property holds the characters from the position following the last match to the end of a string.

$1...$9

   

x

x

   

x

x

x

x

 

Read-only

 

These properties hold the various back matches to submatches that matched sections of the regular expression in parentheses.

The RegExp object is a global one, and to use properties such as $1 through $9 , which refer to parenthesized submatches, you can just use $1 through $9 in your code. In fact, we've already seen that when we used those properties to reverse the order of three words (from Listing 20-09.html):

 <HTML>      <HEAD>          <TITLE>The Reverser</TITLE>          <SCRIPT LANGUAGE="JavaScript">              <!--             function reversem()              {                  var regexp =/(\w+)\s*(\w+)\s*(\w+)/  document.form1.text1.value = document.form1.text1.value.replace(regexp, graphics/ccc.gif "  ")  }              //-->          </SCRIPT>      </HEAD>          .          .          .  </HTML> 

That's it for our look at regular expressions in this chapter. As you can see, there's a terrific amount of powerand complexityavailable if you need to work with matching and extracting text. In the next chapter, we'll keep going with text handlingand handling other elements such as imagesas we start working with Cascading Style Sheets (CSS) in depth.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net