An instance of RegExp has a number of properties that can be of use to developers:
You don’t typically use the global,ignoreCase,multiline, and source properties because you often already have the data they provide:
The really useful property is lastIndex, which tells you how far the regular expression has traveled along a string before stopping:
In this example, the regular expression reB is looking for the character b. When it is first executed against sToMatch, it finds the b in the first position,position 0; therefore,the lastIndex property is set to 1, which is where the matching picks up when exec() is called again. When it’s called again, the expression finds the b in position 1, which sets lastIndex to 2. When called a third time, it finds the b in position 17, setting lastIndex to 18, and so on.
If you want the matching to start at the beginning again, you can always set lastIndex to 0:
With the change in this code,both calls to exec() find the b in position 0, so both times,the alert displays “1” as the value of lastIndex.
The static RegExp properties apply to all regular expressions in scope.These properties are also unique because they each have two names: a verbose name and a short name beginning with a dollar sign. The properties are listed in the following table.
These properties can be used to tell you specific information about the match just completed usingexec() or test(). Example:
var sToMatch = “this has been a short, short summer”;
This example illustrates how the various properties are used:
You can also use the short names for these properties, although you must use the bracket notation for most of them because some names use illegal ECMAScript syntax:
Keep in mind that every time exec() or test() is called,all these properties (except multiline) are reset. Example:
Here, a second regular expression,reLong,is used after reShort. All the RegExp properties are set to new values.
The multiline property is a different type of property because it doesn’t depend on the last executed match.Instead,it sets the m option for every regular expression in scope:
When this code completes execution, arrWords contains “second”, “fourth”, and “sixth”, just as if the m option is used in the regular expression.
Java Script Related Tutorials
|Adv Java Tutorial||Core Java Tutorial|
|Java-Springs Tutorial||Java Servlets Tutorial|
|EJB(Enterprise JavaBeans) Tutorial||JavaServer Faces (JSF) Tutorial|
|Java Swing Tutorial||Java Tutorial|
|JavaMail API Tutorial||Java 8 Tutorial|
|Java XML Tutorial||Java Bean Utils Tutorial|
|The Java Debugger (JDB) Tutorial||Java.math Package Tutorial|
Java Script Related Interview Questions
|Java Script Interview Questions||Adv Java Interview Questions|
|Core Java Interview Questions||Java-Springs Interview Questions|
|Java Servlets Interview Questions||EJB(Enterprise JavaBeans) Interview Questions|
|JavaServer Faces (JSF) Interview Questions||Java Swing Interview Questions|
|Java 8 Interview Questions||Java XML Interview Questions|
|JavaFX Interview Questions||The Java Debugger (JDB) Interview Questions|
Java Script Related Practice Tests
|Java Script Practice Tests||Adv Java Practice Tests|
|Core Java Practice Tests||Java-Springs Practice Tests|
|Java Servlets Practice Tests||EJB(Enterprise JavaBeans) Practice Tests|
|JavaServer Faces (JSF) Practice Tests||Java 8 Practice Tests|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.