JavaScript: RegExp Object

By Xah Lee. Date: . Last updated: .

There are 2 ways to use regex for string pattern matching. One is using string's methods. 〔➤see JavaScript String Regex Methods〕 The other way is creating a regex object, and use the regex object's methods.

Creating a RegExp Object

RegExp in JavaScript is technically a “object”. 〔➤see JavaScript: What's Object?

To create a regex object, there are 2 ways:

Note: when using new RegExp("pattern"), you need to use double backslash in your string argument for any single backslash in your regex. Because, single backslash is used in string to escape the next character. So, when you use double backslash, the regex will just get a single backslash.

// remember to double backslash when using regex constructor

var myRegex1 = new RegExp("\\d+"); // one or more digit
console.log(myRegex1.test("333")); // true. match found

var myRegex2 = new RegExp("\d+"); // WRONG
console.log(myRegex2.test("333")); // false

RegExp Object Methods

MethodMeaning
regex.test(string)Return true or false. 〔➤see JavaScript: RegExp.prototype.test
regex.exec(string)Return a array of captured patterns, or null. 〔➤see JavaScript: RegExp.prototype.exec

RegExp Object Properties

RegExp object has these properties. The most importat one is lastIndex.

PropertyMeaning
sourcethe regex (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 regex 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 regex object properties

var myText = 'a x1 X2 c';

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

var myResult = myRegex.exec(myText);

// print regex properties

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

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

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

// print result

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

Regex Result Object Properties

PropertyMeaning
indexthe beginning position of first occurance.
inputthe input string.

Example:

// example of regex result properties

var myText = 'a x1 X2 c';

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

var myResult = myRegex.exec(myText);

// print result properties

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

Reference

ES5 §15#sec-15.10

Method Reference

  1. JavaScript: Array Methods
  2. JavaScript: String Methods
  3. JavaScript: String Methods for Regex
  4. JavaScript: RegExp Object
  5. JavaScript: Regex Syntax
  6. JavaScript Date Object
  7. JavaScript: JSON Methods
  8. JavaScript: Math Methods
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.