admin管理员组文章数量:1022804
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
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
Share
Improve this question
asked Dec 11, 2019 at 13:36
RoyBlunkRoyBlunk
6110 bronze badges
2
- What is the type of possible example strings? Is it an array? – Mayank Patel Commented Dec 11, 2019 at 13:39
- Better suited for codegolf.stackexchange. – nice_dev Commented Dec 11, 2019 at 13:55
6 Answers
Reset to default 3You 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");
}
you could use string.match method
let str = "log";
var re = "^(log|del|c|cd)$";
var found = str.match(re);
if (found) ....
You could use the includes
method on an array...
let str = "log";
let arr = ["log","del","c","ce"];
console.log(arr.includes(str));
// Or as a single statement...
console.log(["log","del","c","ce"].includes(str));
Alternatively you can use indexOf.
let stringToMatch = "log";
let matches = ["ce", "c", "del", "log"];
console.log(matches.indexOf(stringToMatch) !== -1);
You can use an array and its includes() function call:
let str = "log";
let criteria = ["log", "del", "c", "ce"];
console.log(criteria.includes(str));
You could have matching factory and then use it in any place where you need to do multiple matches.
const matchingFactory = (string) => new Proxy({}, {
get(obj, key, receiver) {
return (() => !!string.match(new RegExp(key, 'i')))()
}
})
And then use it in any module or place in your code in the following way:
const _ = matchingFactory('log');
Then you could shorten your example to
console.log(_.log || _.del || _.c || _.ce); //Return if str has a match
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
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
Share
Improve this question
asked Dec 11, 2019 at 13:36
RoyBlunkRoyBlunk
6110 bronze badges
2
- What is the type of possible example strings? Is it an array? – Mayank Patel Commented Dec 11, 2019 at 13:39
- Better suited for codegolf.stackexchange. – nice_dev Commented Dec 11, 2019 at 13:55
6 Answers
Reset to default 3You 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");
}
you could use string.match method
let str = "log";
var re = "^(log|del|c|cd)$";
var found = str.match(re);
if (found) ....
You could use the includes
method on an array...
let str = "log";
let arr = ["log","del","c","ce"];
console.log(arr.includes(str));
// Or as a single statement...
console.log(["log","del","c","ce"].includes(str));
Alternatively you can use indexOf.
let stringToMatch = "log";
let matches = ["ce", "c", "del", "log"];
console.log(matches.indexOf(stringToMatch) !== -1);
You can use an array and its includes() function call:
let str = "log";
let criteria = ["log", "del", "c", "ce"];
console.log(criteria.includes(str));
You could have matching factory and then use it in any place where you need to do multiple matches.
const matchingFactory = (string) => new Proxy({}, {
get(obj, key, receiver) {
return (() => !!string.match(new RegExp(key, 'i')))()
}
})
And then use it in any module or place in your code in the following way:
const _ = matchingFactory('log');
Then you could shorten your example to
console.log(_.log || _.del || _.c || _.ce); //Return if str has a match
本文标签: javascriptCheck multiple strings shorterjsStack Overflow
版权声明:本文标题:javascript - Check multiple strings shorter, JS? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745577370a2157103.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论