JS: RegExp.prototype

By Xah Lee. Date: . Last updated: .

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

[see JS: RegExp Object]

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

Type of RegExp.prototype is object.

[see JS: 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 JS: 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

Return true or false.

[see JS: RegExp.prototype.test]

RegExp.prototype.exec

Returns a array of captured groups.

[see JS: 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 JS: 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. JS: RegExp Tutorial
  2. JS: String Methods for RegExp
  3. JS: RegExp Object
  4. JS: RegExp Constructor
  5. JS: RegExp.prototype
  6. JS: RegExp Syntax

String Topic

  1. JS: Default Charset/Encoding
  2. JS: String Code Unit vs Code Point
  3. JS: Unicode Character Escape Sequence
  4. JS: Allowed Characters in Identifier
  5. HTML: Allowed Characters in id Attribute
  6. HTML: Character Sets and Encoding
  7. HTML/XML Entity List

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

  1. JS: String Object
  2. JS: String.prototype
  3. JS: 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
Liket it? Put $1 at patreon.

Or, Buy JavaScript in Depth