-1

I have two if conditions to check a value, the two conditions are the same.

I must use if condition twice, and not if and else if.

Here the code below :

regexp = new RegExp("[458]", "gi");
valuesArray = [5, 8];

for(var i = 0; i < valuesArray.length; i++)
{   
    if(regexp.test(valuesArray[i]) == true)
    {   
        console.log("true : "+valuesArray[i]);
    }

    if(regexp.test(valuesArray[i]) == false)
    {   
        console.log("false : "+valuesArray[i]);
    }
}

The javascript console output is :

true : 5
false : 5
true : 8
false : 8

But this should be (because 5 and 8 are in [458] regexp, so regexp test must return true) :

true : 5
true : 8

So why it doesn't work ? Have you an idea ?

Thank you, cordially.

totoaussi
  • 712
  • 1
  • 11
  • 27
  • 2
    Remove global flag `new RegExp("[458]", "i");` – gurvinder372 Nov 13 '17 at 11:00
  • 2
    Also, use `else` instead of the same condition with opposite logic. Especially when functions inside the condition have side effects (like in this case) – Federico klez Culloca Nov 13 '17 at 11:04
  • And you never need `== true` in a condition. (And `!` rather than `== false` is more idiomatic.) So: `if (regexp.test(...)) { /* ... */ } else { /* ... }` – T.J. Crowder Nov 13 '17 at 11:04
  • I rarely use `!` in `if`s. I find they're too easy to overlook. So I tend to always use `== false` @T.J.Crowder – Liam Nov 13 '17 at 11:06
  • @gurvinder372 thank you for the doc, so if i understand, for the 5 value, the second if condition check from 8 value instead of 5 value, that's right ? – totoaussi Nov 13 '17 at 11:07
  • 1
    @Liam: Beware that `!condition` and `condition == false` are not the same thing. With respect, if people are missing the `!` when reading the code, they need to be paying more attention. :-) But naturally you can do what you like, I'm talking about what the vast majority do. – T.J. Crowder Nov 13 '17 at 11:08
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test Using test() on a regex with the global flag If the regex has the global flag set, test() will advance the lastIndex of the regex. A subsequent use of test() will start the search at the substring of str specified by lastIndex (exec() will also advance the lastIndex property). – totoaussi Nov 13 '17 at 11:18

1 Answers1

0

try with regexp = new RegExp("[458]", "i"); instead of regexp = new RegExp("[458]", "gi");

because

g
global match; find all matches rather than stopping after the first match
i
ignore case

regexp = new RegExp("[458]", "i");
valuesArray = [5, 8];

for (var i = 0; i < valuesArray.length; i++) {
  if (regexp.test(valuesArray[i]) == true) {
    console.log("true : " + valuesArray[i]);
  }

  if (regexp.test(valuesArray[i]) == false) {
    console.log("false : " + valuesArray[i]);
  }
}
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
  • why? An explanation of the consequences of doing this would be helpful here. – Liam Nov 13 '17 at 11:04
  • 2
    Better yet, don't answer obviously-duplicate questions. Find the dupetarget and vote to close instead (perhaps with a comment saying how the target applies). – T.J. Crowder Nov 13 '17 at 11:08
  • Using test() on a regex with the global flag If the regex has the global flag set, test() will advance the lastIndex of the regex. A subsequent use of test() will start the search at the substring of str specified by lastIndex (exec() will also advance the lastIndex property). – totoaussi Nov 13 '17 at 11:17