我有一个 2char 字符串数组。我只需要 console.log 与“00”和“01”不同的内容
有人告诉我,下面的代码会跳过“00”字符串,但我不明白它是如何工作的,所以我无法修改它来跳过“01”字符串:
if (!/^0+$/.test(onestring)) continue;
我的代码:
var ar = ["a3","00","12","01","ab","7f"];
for (var i = 0; i < ar.length; i++) {
if (!/^0+$/.test(ar[i])) console.log(ar[i]);
}
我不太明白 /^0+$/
的作用以及如何修改它以包含“01”。
最佳答案
这是一个正则表达式,将匹配每个由一个或多个零组成的字符串。
根据您打算做什么,以下是一些表达式:
匹配以
00
或01
开头的任何字符串:if (!/^0[01]/.test(somestring))
匹配示例:
-00
-01
-00xxxx
-01xxxx
仅匹配
00
和01
:if (!/^0[01]$/.test(somestring))
匹配
0
、00
和01
:
if (!/^0[01]?$/.test(somestring))
关于javascript排除 "00"和 "01"字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26391386/