Search code examples
javascriptregexpostman

For Postman test script match string with regex


Postman response is as below: '''{ "error" : "validation error at #/test/TC_1594792360026/test" }'''

I would like to verify this error message string. Digits are changed every time so I want to use it regex for that. I think regex [0-9]+ should work. But I don't know how to frame it in Postman.

I am using it in postman as below: str t = "validation error at #/test/[0-9]+/test";

pm.expect(t).match(jsonData.error)

and Postman throws an error as "TypeError: e.exec is not a function"


Solution

  • The correct syntax to use regex in the assertion would be:

    pm.expect(jsonData.error).to.match(/validation error at #\/test\/TC_[0-9]+\/test/)

    Each assertion getter need to start with .to and you need to have a valid regex string inside the .match() method.