我正在寻找一种解决方案来搜索字符串中给定字符的存在。这意味着如果字符串中存在任何给定字符,它应该返回 true。
现在我用数组和循环来做这件事。但老实说我觉得这不是一个好方法。那么有没有最简单的方法,无需数组或循环?
var special = ['$', '%', '@'];
var mystring = ' using it to replace VLOOKUP entirely.$ But there are still a few lookups that you are not sure how to perform. Most importantly, you would like to be able to look up a value based on multiple criteria within separate columns.';
var exists = false;
$.each(special, function(index, item) {
if (mystring.indexOf(item) >= 0) {
exists = true;
}
});
console.info(exists);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
最佳答案
尝试使用正则表达式
var patt = /[$%@]/;
console.log(patt.test("using it to replace VLOOKUP entirely.$ But there are still a few lookups that you are not sure how to perform. Most importantly, you would like to be able to look up a value based on multiple criteria within separate columns."));
关于javascript - 如何查找字符串中存在的任何特定字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46341587/