JavaScript: RegExp.prototype

By Xah Lee. Date: . Last updated: .

RegExp.prototype is the value of the property key "prototype" of the RegExp object.

〔➤see JavaScript: RegExp Object

console.log( RegExp.hasOwnProperty ( "prototype" ) ); // true

Type of RegExp.prototype is object.

〔➤see JavaScript: Data Types

// type of RegExp.prototype
console.log ( typeof RegExp.prototype === "object" ); // true
console.log ( Object.prototype.toString.call( RegExp.prototype ) === "[object Object]" ) // true

Parent of RegExp.prototype is Object.prototype.

// parent of RegExp.prototype
console.log ( Object.getPrototypeOf ( RegExp.prototype ) === Object.prototype ); // true

〔➤see JavaScript: Prototype and Inheritance

RegExp.prototype is the parent of all RegExp instances.

console.log ( Object.getPrototypeOf ( /abc/ ) === RegExp.prototype ); // true

The purpose of RegExp.prototype object is to provides methods and properties useful for all regexp instances

Properties

Function Properties

  1. Exp.prototype.test (str)
  2. Exp.prototype.exec (str)

Value Properties

  1. source
  2. global
  3. ignoreCase
  4. multiline
  5. lastIndex

Function Properties

RexExp.prototype.test

〔➤see JavaScript: RegExp.prototype.test

RexExp.prototype.exec

〔➤see JavaScript: RegExp.prototype.exec

Value Properties

RegExp object the following value properties. The most importat one is lastIndex.

PropertyMeaning
sourcethe RegExp (read only)
globalboolean. whether there's the global flag g in regex. (read only)
ignoreCaseboolean. whether there's the ignore case flag i in regex. (read only)
multilineboolean. whether there's the multi-line flag m in regex. (read only)
lastIndexWhen global flag g is used, the value is the position of end of last match. Next invocation of the method with the same RegExp object will start from here. (you may want to reset this to 0 in a loop.) When global flag is not used, this value is always 0.

Example:

// example of RegExp object properties

var myText = 'a x1 X2 c';

var myRE = new RegExp('\\w\\d+', "ig");

var myResult = myRE.exec(myText);

// print RegExp properties

console.log(myRE.source);    // \w\d+

console.log(myRE.global);     // true
console.log(myRE.ignoreCase); // true
console.log(myRE.multiline);  // false

console.log(myRE.lastIndex); // 4

// print result

console.log(myResult); // [ 'x1', index: 2, input: 'a x1 X2 c' ]

RegExp Result Object Properties

PropertyMeaning
indexthe beginning position of first occurance.
inputthe input string.
// example of RegExp result properties

var myText = 'a x1 X2 c';

var myRE = new RegExp('\\w\\d+', "ig");

var myResult = myRE.exec(myText);

// print result properties

console.log ( myResult.index); // 2
console.log ( myResult.input); // a x1 X2 c

Reference

ECMAScript® 2016 Language Specification#sec-regexp.prototype

RegExp Topic

  1. JavaScript: RegExp Tutorial
  2. JavaScript: String Methods for RegExp
  3. JavaScript: RegExp Object
  4. JavaScript: RegExp Constructor
  5. JavaScript: RegExp.prototype
  6. JavaScript: RegExp Syntax

String Topic

  1. JavaScript: Default Charset/Encoding
  2. JavaScript: String is 16-Bit Unit Sequence
  3. JavaScript: Unicode Character Escape Sequence
  4. JavaScript: Allowed Characters in Identifier
  5. HTML: Allowed Characters in id Attribute
  6. HTML: Character Sets and Encoding
  7. HTML XML Entities

  1. JavaScript: Template String
  2. JavaScript: Convert String to Number
  3. JavaScript Encode URL, Escape String
  4. JavaScript: Format Number
  5. JavaScript: JSON Object

  1. JavaScript: String Object
  2. JavaScript: String.prototype
  3. JavaScript: String Constructor

Object Reference

  1. JavaScript: Object Object
  2. JavaScript: Array Object
  3. JavaScript: Function Object
  4. JavaScript: String Object
  5. JavaScript: RegExp.prototype
  6. JavaScript: Date Object
  7. JavaScript: Set Object
  8. JavaScript: Map Object
  9. JavaScript: JSON Object
  10. JavaScript: Math Object
  11. JavaScript: Reflect Object
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.