Javascript RegExp Objects - Properties and Methods


  • In javascript, Regular expressions are used to perform pattern matches in strings.
  • Can be created by
  • i) Literal syntax. For example, to match 10 digit numbers : var mobileNumber = /\d{10}/.
  • ii)With RegExp constructor function. For example var mobileNumber = new RegExp("\\d{10}", "g").

Javascript RegExp Objects Property

specifies the function that creates an object's prototype.
Name Description Version
constructor Implemented in JavaScript 1.1
global Whether or not to test with the regular expression or not. Implemented in JavaScript 1.2
ignoreCase Whether case is to be ignored during pattern matching in a string Implemented in JavaScript 1.2
input The string against which a regular expression is matched. Implemented in JavaScript 1.2
lastIndex Specifies the index at which to start the next match. Implemented in JavaScript 1.2
lastMatch The last matched characters. Implemented in JavaScript 1.2
multiline Whether search in strings should be performed across multiple lines. Implemented in JavaScript 1.2
prototype Use to add new properties and methods to all instances of a class.
Implemented in JavaScript 1.1
rightContext The substring following the most recent match. Implemented in JavaScript 1.2
source A read-only property that contains the text of the pattern. Implemented in JavaScript 1.2

Javascript RegExp Objects Methods

Name Description Version
compile Use to execute the search for a match in a specified string. Implemented in JavaScript 1.2
exec Executes a search for a match in its string parameter. Implemented in JavaScript 1.2
test Executes a search for a match between a regular expression and a specified string Implemented in JavaScript 1.2
toSource Use to get a string representation (source code) of the object. Implemented in JavaScript 1.3
toString Represent the source code of the specified object. Implemented in JavaScript 1.1

Previous: JavaScript: Regular Expression
Next: JavaScript constructor Property: RegExp Object

Test your Programming skills with w3resource's quiz.

JavaScript: Tips of the Day

Sort Number Arrays

JavaScript arrays come with a built-in sort method. This sort method converts the array elements into strings and performs a lexicographical sort on it by default. This can cause issues when sorting number arrays. Hence here is a simple solution to overcome this problem.

[0,10,4,9,123,54,1].sort((a,b) => a-b);
>>> [0, 1, 4, 9, 10, 54, 123]

You are providing a function to compare two elements in the number array to the sort method. This function helps us the receive the correct output.

Ref: https://bit.ly/3adlwNy