How to use regex in AngularJS $httpBackend ExpectGET


I'm passing a url as a parameter in a $resource GET request. Angular is url encoding this parameter and matching the request in the $httpBackend.expectGET method is finicky.

I see that you can use regular expressions to match the expected request but can't get it to work. I'd like to match the resource location but with any "uri" query string parameter value.


var resource = $resource('../api/lookup');
resource.get({ uri: "" }, function (data) {
  // do something


// mock out the $httpBackend response

$httpBackend.expectGET(/\.\.\/api\/lookup\?uri=.*/)).respond(200, { Response: "a response" });

// call my test method here

// ensure the $httpBackend work is done

// do assertions

Karma Output

Error: Unexpected request: GET ../api/lookup?

Expected GET /../api/lookup?uri=.*/

Can anyone see why my regex isn't being matched?


Even after improving the regex as suggested I couldn't get this working using the '$var' injection syntax. I rewrote the tests, injecting the dependencies with the inject function and ensuring the order of my test arrangement was correct. Now it's working like a charm!

Problem courtesy of: daddywoodland


You need to escape the question mark in your regex.

Without escaping it, you're just marking the token prior to it as optional.

> test = "../api/lookup?";

> test.match(/\.\.\/api\/lookup?uri=.*/);

> test.match(/\.\.\/api\/lookup\?uri=.*/);
Solution courtesy of: John Ledbetter


You need to escape the question mark with a double slash \\?

$httpBackend.expectGET(/\.\.\/api\/lookup\\?uri=.*/)).respond(200, { Response: "a response" });

additionally you might want to write your regular expression like this:

new RegExp('../api/lookup\\?uri=.*')

the // syntax has its difficulties when an escaped slash is followed by a modifier

Discussion courtesy of: LanderV

This recipe can be found in it's original form on Stack Over Flow.