我需要一个正则表达式,可以匹配输入字段中的0或1,而不是其他任何字符,既不是数字也不是字母。该如何实现?
var input; // Your input string assume it is populated elsewhere
if (input === '0' || input === '1') {
// Pass
}
/^[01]$/
^
和结束锚点$
。0|1
表达式应该可以运行。/^(0|1)$/
示例:
/^(0|1)$/.test(0); // true
/^(0|1)$/.test(1); // true
/^(0|1)$/.test(2); // false
/^(0|1)$/.test(1001) // false
if (x===0||x===1)
有什么问题? - elclanrs==
与0
进行比较时,我相信您也会匹配null
和其他假值。 - Tom Pietrosanti