skip to Main Content

Can you tell me what I am doing wrong? I am looking to set property pAPIcall_Filter to true if getROL_ID starts with an integer.

    if (getROL_ID.search(/^d/)){
        log.debug("Starts with an integer ");
        next.setProperty('pAPIcall_Filter', true);
    } else {
        log.debug("Does not start with an integer ");
        next.setProperty('pAPIcall_Filter', false);
    }   

Its not working as designed, as seen in the screenshot.
I am sending these two values, KANEJ2 and 573871, the condition thinks they both start with an integer.

2

Answers


  1. Since search returns -1 (which is not zero, nor falsy), this is a true expression.

    You should use:

    if (getROL_ID.match(/^d/)) { ... }
    

    Or, better yet:

    if (/^d/.test(getROL_ID)) { ... }
    

    If you want to correct your logic, compare the result to -1:

    if (getROL_ID.search(/^d/) !== -1) {
      log.debug("Starts with an integer ");
      next.setProperty('pAPIcall_Filter', true);
    } else {
      log.debug("Does not start with an integer ");
      next.setProperty('pAPIcall_Filter', false);
    }  
    
    Login or Signup to reply.
  2. A simple approach is to check a static string.

    Here is an example.

    let getROL_ID = 'KANEJ2'
    let b = '0123456789'.includes(getROL_ID[0])
    
    console.log(b)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search