Understanding the RegExp Object - Java Script

A regular expression in JavaScript is an object just like everything else.You already know that regular expressions are represented by the RegExp object, and you also know that it has methods,which have already been discussed in this chapter. But the RegExp object also has properties, both on the constructor and on instances of RegExp. Both sets of properties change as patterns are created and tested.

Instance properties

An instance of RegExp has a number of properties that can be of use to developers:

  • global — A Boolean value indicating whether or not the g option has been set
  • ignoreCase — A Boolean value indicating whether or not the i option has been set
  • lastIndex — An integer representing the character position where the next match will be attempted (only filled after using exec() or test(), otherwise is 0)
  • multiline — A Boolean value indicating whether the m option has been set
  • source —The string source of the regular expression. For example,the expression /[ba]*/ returns “[ba]*” as its source.

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.

Static properties

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.

Static properties

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:

  • The input property is always equal to the string being tested.
  • RegExp.leftContext contains everything before the first instance of “short” and RegExp.rightContext contains everything after the first instance of “short”.
  • The lastMatch property contains the last string that matches the entire regular expression, which is “short”.
  • The lastParen property contains the last matched group, which in this case is “s”.

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.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Script Topics