Forum Discussion

mcaramto's avatar
mcaramto
Occasional Contributor
10 days ago
Solved

Regex Checkpoint Javascript HELP!

Hello, 

Can anyone help me how to use regular expression for checkpoints, I've been trying to use it, but my checkpoint keeps failing. I think I am doing it wrong. 

I am trying to check if the property of an object returns an integer. 

My code is something like this:

aqObject.CheckProperty(Object.Test, "Value", cmpMatches , RegExp("\d+")) 

Result: 

Thanks. 

  • rraghvani's avatar
    rraghvani
    Icon for Champion Level 3 rankChampion Level 3

    If using JavaScript, use typeof. For example,

    let myvalue = 10; // test with let myvalue = "mystring"
    aqObject.CompareProperty(typeof myvalue, cmpEqual, "number", true, lmError);
    

    The typeof operator returns a string indicating the type of the operand's value. You can then check to see if the return value equals "number".

    Regular Expression is used for string matching and manipulation. For example, if a string contains text and an integer value, you can define a regex string pattern to extract the integer value, and then store this into a variable. 

    • mcaramto's avatar
      mcaramto
      Occasional Contributor

      Thanks for replying.

      Could be the case, it does work when doing checkpoints on string only values. And yes, storing it in a variable and doing conditional regex checking is another way of doing it. 

      Although I wanted to keep it simple by just using the "aqObject checkpoint", instead of writing few more lines of code. Hope smartbear adds this option. 

  • torus's avatar
    torus
    Frequent Contributor

    I don't know if you need 'RegExp' before the regular expression. 
    Can you try just putting "-?\b\d+\b" in the last argument instead of RegExp("\d+"). Does that work?

    • mcaramto's avatar
      mcaramto
      Occasional Contributor

      Thanks for replying, I tried it and it still failing.  Here is the result. 

      • torus's avatar
        torus
        Frequent Contributor

        It looks like Hassan_Ballan is correct in that it only works for string values unfortunately.