使用 JS .match() 从字符串中提取数字,然后使用结果来测试相等性

Using JS .match() to extract number from string, then use the result to test equality

希望有人能帮助我,因为我对 JS 还很陌生。我需要从 2 个字符串中提取一个数字,然后测试结果是否相等。

例如

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

要提取数字,我使用以下代码,

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

我对 test2 变量做同样的事情,结果也是 7。

然而,当我使用

测试相等性时
test1.match(/\d+/) === test2.match(/\d+/) it evaluates to false.  

我试图在 if 语句中使用这个条件,但无法让它工作,例如

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

我做错了什么或者有更好的方法来实现这个目标吗?

谢谢,

match returns 一个数组。要比较匹配值,请使用:

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

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