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. RegExp.prototype.test ( str )
  2. RegExp.prototype.exec ( str )
  3. RegExp.prototype.toString ( )
  4. RegExp.prototype [ Symbol.match ] ( str ) (ES2015)
  5. RegExp.prototype [ Symbol.replace ] ( str, replaceValue ) (ES2015)
  6. RegExp.prototype [ Symbol.search ] ( str ) (ES2015)
  7. RegExp.prototype [ Symbol.split ] ( str, limit ) (ES2015)

Value Properties

  1. RegExp.prototype.source
  2. RegExp.prototype.global
  3. RegExp.prototype.ignoreCase
  4. RegExp.prototype.multiline
  5. RegExp.prototype.lastIndex
  6. RegExp.prototype.flags (ES2015)
  7. RegExp.prototype.sticky (ES2015)
  8. RegExp.prototype.unicode (ES2015)

Function Properties

RegExp.prototype.test

Returns true or false.

〔►see JavaScript: RegExp.prototype.test

RegExp.prototype.exec

Returns a array of captured groups.

〔►see JavaScript: RegExp.prototype.exec

RegExp.prototype [ Symbol.match ] ( str ) (ES2015)

ECMAScript® 2016 Language Specification#sec-regexp.prototype-@@match

RegExp.prototype [ Symbol.replace ] ( str, replaceValue ) (ES2015)

ECMAScript® 2016 Language Specification#sec-regexp.prototype-@@replace

RegExp.prototype [ Symbol.search ] ( str ) (ES2015)

ECMAScript® 2016 Language Specification#sec-regexp.prototype-@@search

RegExp.prototype [ Symbol.split ] ( str, limit ) (ES2015)

ECMAScript® 2016 Language Specification#sec-regexp.prototype-@@split

Value Properties

PropertyRead OnlyMeaning
sourceread onlythe regex pattern string.
globalread onlytrue if there's the global flag g in regex.
ignoreCaseread onlytrue if there's the ignore case flag i in regex.
multilineread onlytrue if there's the multi-line flag m in regex.
unicoderead onlytrue if there's the unicode flag u in regex. (ES2015)
stickyread onlytrue if there's the sticky flag y in regex. (ES2015)
flagsread onlyReturns a string of all used flags. e.g. "gimuy" (ES2015)
PropertyRead OnlyMeaning
lastIndexread/writeWhen 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.

〔►see JavaScript: RegExp Syntax

Example:

// example of RegExp.prototype value properties

var txt = 'a x1 x2 c';

var regex = new RegExp('x2', "ig");

var result = regex.exec(txt);

// print RegExp properties

console.log ( regex.source);    // x2

console.log ( regex.global);     // true
console.log ( regex.ignoreCase); // true
console.log ( regex.multiline);  // false
console.log ( regex.flags); // "gi"

console.log ( regex.lastIndex); // 7

// print result
console.log ( result); // [ 'x2', index: 5, input: 'a x1 x2 c' ]

RegExp Result Object Properties

PropertyMeaning
indexthe beginning position of first occurrence.
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

JS Object Reference

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