0

For a REST call I am getting response back. I am getting response as:

call_name response_code OK

Now I want to give an if condition if my response code is either 200 or 201. So what is the regex pattern that I should use?

Example:

response.body = getOtp 200 OK

if(response.body.match()) {
    console.log("hello");
}
talemyn
  • 7,822
  • 4
  • 31
  • 52
user7350714
  • 365
  • 1
  • 6
  • 20
  • should it be `if {} else {}` condition for `200` and `201` code? – RomanPerekhrest Feb 23 '17 at 19:33
  • 1
    How are you making your request. You should be able to access the status code directly without using regex. see http://stackoverflow.com/questions/5344145/how-to-get-response-status-code-from-jquery-ajax and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status – Theo Feb 23 '17 at 20:04

4 Answers4

4

You can use character set to match either 200 or 201 codes:

var response = {body:"getOtp 200 OK"};

if(response.body.match(/20[01] OK/)){
    console.log("Matching");
}
SLePort
  • 15,211
  • 3
  • 34
  • 44
2
if(response.body.match(/20[01]/) && reponse.body.match(/\bOK\b/)){
    console.log("hello");
}

Hope it helps. Didn't get to test it, but It should work just fine.

  • 1
    I already posted this before I know he wanted to match 201 as well. I already edited it –  Feb 24 '17 at 11:14
0
if(response.body.match(/200|201/){
    console.log("hello");
}
archana
  • 1,282
  • 8
  • 11
0

I had the same question as OP. However I ran into a problem:

"___.match is not a function"

Thought I'd share what solved it for me:


if (String(response.status).match(/20[01]/)) {
    // do stuff
}

Hoppjerka
  • 128
  • 1
  • 9