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

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

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]

Purpose

Purpose of RegExp.prototype is to provide methods and properties useful for all regexp instances.

RegExp.prototype is the parent of all RegExp instances.

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

Properties

Function Properties

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

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

RegExp.prototype [ Symbol.replace ]

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

RegExp.prototype [ Symbol.search ]

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

RegExp.prototype [ Symbol.split ]

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

Value Properties

PropertyRead OnlyMeaning
sourceread onlythe regex pattern as 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

const txt = "love cats";

const regex = new RegExp("cat", "ig");

const result = regex.exec(txt);

// print RegExp properties
console.log ( regex.source === "cat"); // true
console.log ( regex.global);     // true
console.log ( regex.ignoreCase); // true
console.log ( regex.multiline);  // false
console.log ( regex.flags === "gi"); // true

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

// print result
console.log ( result); // [ 'cat', index: 5, input: 'love cats' ]

RegExp Result Object Properties

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

const myText = 'a x1 X2 c';

const myRe = new RegExp('\\w\\d+', "ig");

const 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: RegExp Syntax
  3. JS: Regex Find/Replace
  4. JS: RegExp Object
  5. JS: RegExp Constructor
  6. JS: RegExp.prototype

String Topic

  1. JS: Source Code 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

  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 $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon