Properties of the RegExp Objects

The regular expression objects have the following properties.

PropertiesDescription
globalIf this property is false, which is the default, the search stops when the first match is found. Set this to true if you want all matches.
ignoreCaseCase sensitive match or not, defaults to false.
multilineSearch matches that may span over more than one line, defaults to false.
lastIndexThe position at which to start the search, defaults to 0.
sourceContains the regexp pattern.

None of these properties, except for lastIndex, can be changed once the object has created.

The first three parameters represent the regex modifiers. If you create a regex object using the constructor, you can pass any combination of the following characters as a second parameter:

  • "g" for global
  • "i" for ignoreCase
  • "m" for multiline

These letters can be in any order. If a letter is passed, the corresponding modifier is set to true. In the following example, all modifiers are set to true:

var re = new RegExp('j.*t', 'gmi');

Let's verify:

re.global;

true

Once set, the modifier cannot be changed:

re.global = false;
re.global

true

To set any modifiers using the regex literal, you add them after the closing slash.

var re = /j.*t/ig;

re.global
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +