I'm looking for a regular expression pattern to match a string that contains all of a list of required characters.
For example, if my requirements are "abc":
- Match:
"abacus","back","cab". - Don't match:
"abs","car","banana".
So far, I've come up with these (non-regex) methods:
function testA(requiredChars, checkString) {
return requiredChars.split('').every(char => checkString.indexOf(char) !== -1)
}
function testB(requiredChars, checkString) {
for (let char of requiredChars.split('')) {
if (checkString.indexOf(char) == -1) return false
}
return true
}
tests = [ 'abacus', 'back', 'cab', 'abs', 'car', 'banana' ]
tests.forEach(word => {
console.log(word, testA('abc', word), testB('abc', word))
})
// abacus true true
// back true true
// cab true true
// abs false false
// car false false
// banana false false
I like that the first one is smaller, but unfortunately the second one is faster. Can this be done faster with regex, or should I just quit while I'm ahead?