![](/img/trans.png)
[英]How to validate multiple checkbox to check atleast one in jquery/javascript?
[英]Check if 1 of multiple chars are in a string, return true if atleast one is
所以這會檢查所有內容是否都在一個字符串中,並且只有在這種情況下才返回 true。
function check(Entry) { var contents = "abcd"; for (var i = 0; i < Entry.length; i++) if (contents.indexOf(Entry.charAt(i)) < 0) return false; return true; } console.log(check("abc")); console.log(check("abe"));
我需要一個 function 來檢查字符串中是否atleast
有一個字符並返回 true。
只需將您當前的 function 修改為:
function hasAny(haystack, needles)
{
for (var i = 0; i < heystack.length; i++)
if (needles.indexOf(heystack[i])) > 0)
return true;
return false;
}
或者,您可以使用組合正則表達式,為您提供不區分大小寫的選項:
function firstMatch(stack, needles, cis = false , m) {
return (m = stack.match((new RegExp('(['+needles+'])', cis ? 'i' : '' ))))
? m.index : -1;
}
function hasAny(stack, needles, cis ) {
return -1 != firstMatch(stack, needles, cis );
}
console.log(firstMatch(a,b)) // -1
console.log(hasAny(a,b)) // false
// Case Insensitive Examples...
console.log(firstMatch(a,b, true)) // 0, which is the index of where the match is in a
console.log(hasAny(a,b, true)) // true
function 檢查字符串中是否至少有一個字符並返回 true
function check(str) { const contents = 'abcd'.split(''); return str.split('').some(c => contents.includes(c)); } console.log(check('abc')); console.log(check('afg')); console.log(check('fgh'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.