I want to check a string if it matches one or more other strings. In my example i just used 4 possible string values. How can i write this code shorter if i have to check for more values?
//Possible example strings: "ce", "c", "del", "log"
let str = "log"; //My input
console.log(str == "log" || str == "del" || str == "c" || str == "ce"); //Return if str has a match
You could use string test here along with an alternation:
var input = "log";
if (/^(?:log|del|c|ce)$/.test(input)) {
console.log("MATCH");
}
else {
console.log("NO MATCH");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With