2

Hope someone can help as I quite new to JS. I need to extract a number from 2 strings then test the result for equality against each other.

For example

var test1 = "7D"
var test2 = "7H"

To extract the numbers I'm using the following code,

test1.match(/\d+/) = result in the console is "7", 

I do the same for the test2 variable and this results in 7 also.

However when I test for equality using

test1.match(/\d+/) === test2.match(/\d+/) it evaluates to false.  

I'm trying to use this condition in an if statement but cannot get it to work, e.g.

if(test1.match(/\d+/) === test2.match(/\d+/)){run some code}

Am I doing something wrong or is there a better way to acheive this?

Thanks,

1 Answers1

3

match returns an array. To compare matched value use:

var b = (test1.match(/\d+/)[0] === test2.match(/\d+/)[0]);
//=> true

Check this Q&A on how to compare arrays in avascript

Community
  • 1
  • 1
anubhava
  • 761,203
  • 64
  • 569
  • 643