DEV Community

Discussion on: Daily Challenge #232 - Regex Pattern

Collapse
 
fluffynuts profile image
Davyd McColl
function regex_contains_all(chars, caseSensitive) {
  return new RegExp(`^[${chars}]*$`, caseSensitive ? undefined : "i");
}
  • it's case-insensitive by default -- the question isn't clear about case sensitivity
  • the stated tests look like they are meant for Python, so here's a self-contained test suite:
function assert(condition) {
    if (!condition) {
        throw new Error("FAIL");
    }
}

var abc = 'abc'
var pattern = regex_contains_all(abc)
assert("bca".match(pattern) !== null);
assert("baczzz".match(pattern) === null);
assert("ac".match(pattern) !== null);
assert("cb".match(pattern) !== null);
Collapse
 
qm3ster profile image
Mihail Malo

You did "string consists only of", not "string includes all of".
The intended result matrix is:

assert(pattern.test("zbca"))
assert(pattern.test("zbaczzz"))
assert(!pattern.test("zac"))
assert(!pattern.test("zcb"))