; If-Var-In way. Case-insensitive.
Ext := "txt"
If Ext In txt,jpg,png
MsgBox,,, % "Foo"
; RegEx way. Case-insensitive. To make it case-sensitive, remove i).
Ext := "txt"
If (RegExMatch(Ext, "i)^(?:txt|jpg|png)$"))
MsgBox,,, % "Foo"
; Array way 1. Array ways are case-insensitive.
Ext := "txt"
If ({txt: 1, jpg: 1, png: 1}.HasKey(Ext))
MsgBox,,, % "Foo"
; Array way 2.
Extensions := {txt: 1, jpg: 1, png: 1}
Ext := "txt"
If (Extensions[Ext])
MsgBox,,, % "Foo"
If-Var-In 是最原生的方式。但是,您应该知道它不是一个表达式,因此它不能是另一个表达式的一部分。
破碎:
SomeCondition := True
Extension := "exe"
If (SomeCondition && Extension In "txt,jpg,png")
MsgBox,,, % "Foo"
Else
MsgBox,,, % "Bar"
正常工作:
SomeCondition := True
Extension := "exe"
If (SomeCondition && RegExMatch(Extension, "i)^(?:txt|jpg|png)$"))
MsgBox,,, % "Foo"
Else
MsgBox,,, % "Bar"
出于同样的原因(即因为它不是表达式),您不能使用 K&R 大括号样式。
正常工作:
Ext := "txt"
If Ext In txt,jpg,png
MsgBox,,, % "Foo"
Ext := "txt"
If Ext In txt,jpg,png
{
MsgBox,,, % "Foo"
}
破碎:
Ext := "txt"
If Ext In txt,jpg,png {
MsgBox,,, % "Foo"
}