[英]How to check by chai if string contains five @?
我有字符串:
const hash = 'dwqdiojqwoidj@2323joij@oindoi2d@dndi2on@diodno@1';
如何檢查此字符串是否恰好包含五個@?
我可以檢查字符串是否包含一個@:
expect(hash).to.include('@');
您可以計算發生次數,然后驗證計數:
const hash = 'dwqdiojqwoidj@2323joij@oindoi2d@dndi2on@diodno@1';
const count = (hash.match(/@/g) || []).length;
expect(count).to.equal(5);
let isValid = 0
for (let i = 0; i < hash.length; i++) {
if (hash.includes("@", i)){
isValid++;
if(isValid === 5) {
// do something (true)
}
} else {
// do something (false)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.