A regular expression object is an instance of the RegExp object. Each regular expression object consists of a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
To create a regular expression object, surround the pattern with forward slashes, and assign the whole var re = /greet/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The re variable can then be used as a parameter in a variety of methods that search for the pattern within some | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Regular expression notation also consists of a number of metacharacters that stand in for sometimes complex var re = /\bgreet\b/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The following table shows a summary of the regular expression notation used in JavaScript 1.2. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
When you create a regular expression, you may optionally wire the expression to work globally (as you | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Once you have established a pattern with the regular expression notation, all the action takes place in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Properties | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Methods | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Creating a regular expression Object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var regExpressionObj = /pattern/ [g | i | gi]; var regExpressionObj = new RegExp(["pattern", ["g" | "i" | "gi"]]); |
constructor | NN 4 IE 4 ECMA 3 |
Read/Write | |
See this property for the Array object. |
global, ignoreCase | |
| |
Returns Boolean true if the regular expression object instance had the g or i modifiers (respectively) set when | |
Example | |
if (myRE.global && myRE.ignoreCase) { ... } | |
Value | |
Boolean value: true | false. |
lastIndex | |
| |
This is the zero-based index value of the character within the string where the next search for the pattern begins. | |
Example | |
myRE.lastIndex = 30; | |
Value | |
Integer. |
source | |
| |
Returns a string version of the characters used to create the regular expression. The value does not include the | |
Example | |
var myREasString = myRE.source; | |
Value | |
String. |
compile( ) | |
compile("pattern"[, "g" | "i" | "gi"]) | |
Compiles a regular expression pattern into a genuine regular expression object. This method is used primarily | |
Parameters | |
| |
Returned Value | |
Reference to a regular expression instance. |
exec( ) | |
exec(string) | |
Performs a search through the string passed as a parameter for the current regular expression pattern. A typical var myRE = /somePattern/; var resultArray = myRE.exec("someString"); | |
Properties of both the static RegExp and regular expression instance (myRE in the example) objects are updated
| |
You can stow away the results of the exec( ) method in a variable, whereas the RegExp property values change | |
If no match is found for a given call to exec( ), it returns null. | |
Parameters | |
| |
Returned Value | |
An array of match information if successful; null if there is no match. |
test( ) | |
test(string) | |
Returns Boolean true if there is a match of the regular expression anywhere in the string passed as a parameter, | |
Parameters | |
| |
Returned Value | |
Boolean value: true | false. |
No comments :
Post a Comment