...

/

Advanced Matchers

Advanced Matchers

Learn how to write expectations using matchers and regular expressions.

Jasmine offers a number of matchers that let us write expressive expectations.

The toMatch method

There are two ways to check whether a string value contains a specified string.

  • We can call the toMatch method with our search string.

  • We can call the toContain method with our search string.

Take a look at the code example below. These two expectations are equivalent:

Press + to interact
expect("<h1>Cafe Americano (dairy-free)</h1>").toMatch("Americano");
expect("<h1>Cafe Americano (dairy-free)</h1>").toContain("Americano");

We can also use the toMatch method to let us match a regular expressionA regular expression is a string representing a search pattern. JavaScript supports regular expressions with the native RegExp class.. In the following code block, notice how we wrap our regular expression with the / symbol, but we do not use quotation marks. In the second example, we use /i, which is ...