我有一个字符串:
expect(hash).to.include('@');
如何检查此字符串是否恰好包含五个@?
我可以检查字符串是否包含一个@:
<form class="form-inline mt-2 mt-md-0">
<input
class="form-control mr-sm-2"
type="text"
name="search"
placeholder="Search"
aria-label="Search"
/>
<button id="search-btn" class="btn btn-outline-success my-2 my-sm-0" type="submit">
Search
</button>
</form>
<ul id="response">
<li>Attack
</li>
<li>Defence
</li>
<li>Strength
</li>
<li>HitPoints
</li>
</ul>
答案 0 :(得分:3)
您可以计算发生次数,然后验证计数:
const hash = 'dwqdiojqwoidj@2323joij@oindoi2d@dndi2on@diodno@1';
const count = (hash.match(/@/g) || []).length;
expect(count).to.equal(5);
答案 1 :(得分:2)
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)
}
}