Если порядок незначителен (numbers
может быть 125,126
o4 126,125
, и вы хотите, чтобы оба соответствовали вашему примеру), вам нужно разбить эти строки на массивы, а затем проверить, соответствуют ли записи вnumbers
все в matches
:
const match = numbers.split(",").every(n => matchesArray.includes(n));
Live Пример:
function test(matches, numbers, expected) {
const matchesArray = matches.split(",");
const match = numbers.split(",").every(n => matchesArray.includes(n));
console.log(`matches: ${matches}`);
console.log(`numbers: ${numbers}`);
console.log(`match: ${match}`);
console.log(!match === !expected ? "=> Good" : "=> ERROR");
}
test(
'128,126,125,124,123,122,118,117,116,115,99,98,97',
'126,125',
true
);
test(
'128,126,125,124,123,122,118,117,116,115,99,98,97',
'125,126', // <== I changed the order here
true
);
test(
'128,126,125,124,123,122,118,117,116,115,99,98,97',
'119,126',
false
);
.as-console-wrapper {
max-height: 100% !important;
}
Если порядок значительный, проще всего будет поставить вокруг них запятые и проверить подстроку:
const match = ("," + matches + ",").includes("," + numbers + ",");
Live Example:
function test(matches, numbers, expected) {
const match = ("," + matches + ",").includes("," + numbers + ",");
console.log(`matches: ${matches}`);
console.log(`numbers: ${numbers}`);
console.log(`match: ${match}`);
console.log(!match === !expected ? "=> Good" : "=> ERROR");
}
test(
'128,126,125,124,123,122,118,117,116,115,99,98,97',
'126,125',
true
);
test(
'128,126,125,124,123,122,118,117,116,115,99,98,97',
'125,126', // <== I changed the order here
false // <== Shouldn't work
);
.as-console-wrapper {
max-height: 100% !important;
}