[JavaScript] Regular Expressions

A regular expression is a pattern of characters that can be used for searching or replacing. The expression might look weird at first, but it will become familiar to you and even you will love it in the end. JavaScript provides “RegExp” object that encapsulate the regular expression features. This post does not explain how to write regular expressions. Rather it focuses on how to use regular expressions in JavaScript.

1. Regular Expression Literals

In JavaScript, regular expressions are specified within a pair of slash (/) characters. Regular expression literals are converted to “RegExp” objects.

// 2 ways to create RegExp objects
var pattern1 = /d{3}/;
var pattern2 = new RegExp("d{3}");


2. String Methods

The “String” object provides methods for pattern matching with regular expressions. String methods will accept a simple string or a regular expression as an argument.

  • match() : searches a string for a match against a regular expression and returns the matching strings as an Array object
  • search() : searches a string for a regular expression and returns the position of the matching string
  • replace() : searches a string for a regular expression and returns a new string with the replaced value
var phoneNumber = "111-222-3333";

var matches = phoneNumber.match(/\d{3,4}/g); // 3 or 4 digits
alert(matches.join()); // 111,222,3333

var lastNumber = phoneNumber.search(/\d{4}$/);
alert(lastNumber); // 8

var newPhoneNumber = phoneNumber.replace(/^\d{3}/, "999");
alert(newPhoneNumber); // 999-222-3333


3. RegExp Object Properties

The RegExp object encapsulate the regular expression and you can check the information of the expression.

  • source : the text of the expression
  • global : whether the “g” modifier is set
  • ignoreCase : whether the “i” modifier is set
  • multiline : whether the “m” modifier is set
  • lastIndex : index at which to start the next match
var pattern1 = /\d{3}/g;
alert(pattern1.source); // \d{3}
alert(pattern1.global); // true
alert(pattern1.ignoreCase); // false
alert(pattern1.multiline); // false
alert(pattern1.lastIndex); // 0


4. exec() Method

The main method of RegExp is “exec()“. It returns the matched text if it finds a match, otherwise it returns null.

  • exec(string)

When “exec()” is called with the global “g” modifier, the lastIndex property is set for the next search.

var phone = "111-222-3333";
var pattern = /\d{3}/g;
while ((number = pattern.exec(phone)) != null) {
  alert(number + ", now at " + pattern.lastIndex);
  // 111, at 3
  // 222, at 7
  // 3333, at 11


5. test() Method

The “test()” method simply returns true if it finds a match.

var greet = "Hello, World";
var pattern1 = /hello/i;
var pattern2 = new RegExp("/Universe/i");

alert(pattern1.test(greet)); // true
alert(pattern2.test(greet)); // false

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s