JavaScript Tutorial/Regular Expressions/RegExp Object

Материал из Web эксперт
Перейти к: навигация, поиск

RegExp()

Syntax



   <source lang="javascript">

var variable = new RegExp(pattern, flags)</source>


The RegExp() object represents a regular expression that is used for pattern matching.

The creation of the object takes pattern and flags parameters.

The pattern is a valid regular expression. The flags are either or both g (global) and i (ignore case).

Properties and Methods of the RegExp() Object

Property/Method Description RegExp.$* Represents multiline RegExp.$& Represents lastmatch RegExp.$_ Represents input RegExp.$` Represents leftContext RegExp.$" Represents rightContext RegExp.$+ Represents lastParen RegExp.$1,$2,...$9 Represents substring of matches compile() Compiles a regular expression exec() Executes the search for a match in a specified string global _Specifies whether to check the expressions against all possible matches ignoreCase Whether case is ignored or not during a string search input String that is matched lastIndex _Specifies the index at which to start matching the next string. lastMatch Last matched characters lastParen The last parenthesized substring match leftContext The substring preceding the most recent match multiline Specifies whether to search on multiple lines rightContext The substring following the most recent match source The string pattern test() Tests for a string match

RegExp,$* (1)

The RegExp,$* property reflects a multiline string search.

This is a Boolean, read-only value that reflects whether or not strings should be searched across multiple lines.

This is the same as using the multiline property.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   <form name="form1">
   <textarea name="mytext" cols="60" rows="8" onChange="getinfo()">
   Change me and click outside to see the result.
   </textarea>
   
</form> </body> </html></source>

RegExp.$1,$2,..$9

The RegExp.$1,$2,..$9 property represents parenthesized substring matches.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript1.2">
   
   </script>
   <form name="form1">
   Enter your 7 digit phone number in the form xxx-xxxx
   


Phone Number (7 digits):<input type="text" name="text1" size=10>

<input type="button" value="Swap" onClick="swap()">


Output: <input type="text" name="text2" size=10 </form> </body> </html></source>

RegExp.$& (2)

The RegExp.$& property represents the last matched characters.

This is the same as using the lastMatch property.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

RegExp,$_ (3)

The RegExp,$_ property represents the input to which a string is matched.

This is the same as using the input property.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   <form name="form1">
   
   

Enter some Text and click outside: <input type="text" name="mytext" size="40" onChange="getinput()">
</form> </body> </html></source>

RegExp.$` (4)

The RegExp.$` property represents the substring preceding the most recent pattern match.

This is the same as using the leftContext property.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

RegExp.$" (5)

The RegExp.$" property represents the substring following the most pattern match.

This is the same as using the rightContext property.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

RegExp.$+ (6)

The RegExp.$+ property represents the last parenthesized substring pattern match.

This is the same as using the lastParen property.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

RegExp.compile()

Syntax



   <source lang="javascript">

regexp.rupile(pattern, flag)</source>


RegExp.exec()

Syntax



   <source lang="javascript">

regexp.exec(string)</source>


RegExp.global

The global property specifies whether or not the g flag is used with the regular expression.

If so, a global pattern match will be performed.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

RegExp.ignoreCase

The ignoreCase property is a flag that informs the user if case is to be ignored during pattern matching or not.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

RegExp.input

The input property represents the string on which the pattern matching is performed.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript1.2">
   
   </script>
   <form name="form1">
   

Enter some Text and click outside: <input type="text" name="mytext" size="40" onChange="getinput()">






<inputtype=name=size=

<inputtype=value=</form> </body> </html></source>

RegExp.lastIndex

The lastIndex property gets the index of where the next match begins.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

RegExp.lastMatch

The lastMatch property represents the last matched characters.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

RegExp.lastParen

The lastParen property represents the last parenthesized substring match.

It returns a string value for the last parenthesized substring.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

RegExp.leftContext

The leftContext property represents the substring preceding the most recent pattern match.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

RegExp.multiline

The multiline property determines whether pattern matching should be performed across multiple lines.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   <form name="form1">
   

<textarea name="mytext" cols="60" rows="8" onChange="getinfo()"> Change me and click outside </textarea>
</form> </body> </html></source>

RegExp.rightContext

The rightContext property represents the substring following the most recent pattern match.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

RegExp.source

The source property represents the text being used for pattern matching.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

RegExp.test()

The test() method tests for a pattern match in a string.

Returns Boolean value true or false.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>