如何通过柴检查字符串是否包含五个@?

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)
    }
}