RegExp Methods

Chapter 12 8 mins

Learning outcomes:

  1. RegExp methods - test()

Introduction

In the previous chapter we discovered the three string methods search(), match() and replace() and how they served different purposes of pattern matching.

In this chapter we shall explore the other basket of options i.e RegExp() methods to test for patterns within strings and extract substrings out of strings based on them.

Regular expression objects

Before starting on with anything, recall that we have seen a constructor way to create regular expressions back in the RegExp Basics chapter. We also know that a constructor throws out an object, which can then have properties and methods on it.

This means that all regular expressions, created literally or from the constructor are in effect instances of the RegExp() function and hence inherit methods from the prototype RegExp.prototype.

For a regular expression, there are two methods available (on the prototype object RegExp.prototype) namely test() and exec(). Both these methods are regexp methods and likewise operate slightly differently than the string methods we saw previously.

We will see what this slight difference actually is as this chapter progresses and how it can be used to fill the gaps introduced otherwise by the string methods.

Test for patterns

The first method which we will be looking over is the method test(). It takes as an argument the string to test the pattern on and returns true or false based on whether it contains the pattern anymore or not.

Let's consider a simple example illustrating the idea.

var str = "Hello World!";
var patt = /hello/;
console.log(patt.test(str)); // false

Since here the string doesn't contain the pattern /hello/, the method returns false.

Similarly we can take a look at another instance where the pattern is actually seen in the string.

var str = "Hello World!";
var patt = /hello/i;
console.log(patt.test(str)); // true

The output of test() here shouldn't at all be any surprising - str does contain the pattern patt and hence we get true returned.

So hopefully both these examples were fairly easy to understand. However what might not be easy and intuitive to understand at first glance is the behavior of this method when the global flag is set on the regular expression.

Consider the code below. The expression has the global g flag set and as we can see the string contains only one match for the pattern.

var str = "Hello World!";
var patt = /hello/ig;
console.log(patt.test(str)); // true

Just as before, we get true returned since the string does contain an occurence of the pattern. However if we recall this method in the next line, surprisingly, we get false returned. Did something chang; did something go wrong? Well nothing as such happened!

var str = "Hello World!";
var patt = /hello/ig;
console.log(patt.test(str)); // true
console.log(patt.test(str)); // false

What happens here is due to the presence of the global flag. It makes the method keep on its searching until the end of the string is reached. As soon as a match is found true is returned and the index after the match is saved for the next searching round. Once the end of the string is reached false is returned.

Let's apply this to our example above and try to make it easier to comprehend.

At the first call of test() the method takes the string str and checks for a match of the pattern patt within it.

It starts at index 0; a match is indeed found and true is returned. The internal index of the method changes to the point to the index after the last match. Now calling it again would make it search from the last position until a new match is found or the end of the string is reached.

Because there exists no more matches in the string after the first match, the method finally reaches the end of the string and returns false.

Once the whole string is analysed by the regexp method, the index resets all the way back to 0 and searching will restart in subsequent calls of the method.

This would mean that if you call test() the third time, it will return true. Fourth time again false; then true, then false and so on.

var str = "Hello World!";
var patt = /hello/ig;
console.log(patt.test(str)); // true
console.log(patt.test(str)); // false
console.log(patt.test(str)); // true

In the following snippet of code what will the lines 4-6 return, in the order shown?

var str = "Hello World! Hello once again!";
var patt = /hello/ig;

console.log(patt.test(str));
console.log(patt.test(str));
console.log(patt.test(str));

How can you confirm that in a string you have at least two occurences of a given regexp pattern?

Well there is no direct way to approach this problem by constructing any expression and thinking it would out of somewhere return the number of its matches in the string provided.

We have to use the regexp method test() to solve this problem. The pattern occuring two times means that both the consecutive calls of test() should return true. In other words this means that the conjunction (using the && operator) of both the outputs of the method should return true as well. So the final code will be:

// suppose str is the test string
// suppose patt is the test pattern
var success = patt.test(str) && patt.test(str)

If success is true it would mean that the string contains the pattern for at least two times.

Long story short summary, the test() method shall be used, just as it says, only when we need to test a string for a given pattern's one or more occurences.