0

我有一个具有特定值的变量,例如var no = 123056.
有没有办法在 的帮助下确定该数字是否包含数字 0 RegExp

4

4 回答 4

3
var no = 123056
var doesZeroExist = (no+"").indexOf('0') > -1;
于 2013-09-13T11:16:15.280 回答
0

尝试这样的事情。

var no = 12045;
var patt1=/0/; // search pattern

if (no.toString().match(patt1) !== null){
    // contains the character 0
    alert("character '0' found")
} else {
    // does not contain 0
    alert("'0' not found")
};

这是一个JSFiddle

于 2013-09-13T11:22:09.790 回答
0

如果你真的想使用 RegExp,是的,有可能:

/0/匹配 0。将 0 替换为任何其他数字,包括 10+

/[035]/匹配 0、3 或 5 之一。将它们替换为您想要的任何数字。

如果您想要一个数字序列,请在+其后添加一个。

/(012)+/将匹配 1 到 012 的无限连续组,例如 012、012012、012012012 ...

/012+/将 01 和 1 匹配到无限数量的 2,例如 012、0122、01222 ...

此外,您可能想要使用的最佳 RegExp 工具:http: //www.debuggex.com/

于 2013-09-13T11:24:25.490 回答
0
var no='123056';
var regex=/0/;
if (no.match(regex) == 0) {
   alert('hey');
}

如果找到 0,这将给您一条警报消息。

于 2013-09-13T11:38:04.097 回答