Javascript: return 如果数组中的所有字符串都存在于另一个字符串中则为真
Javascript: return true if ALL strings inside an array are present in another string
我必须检查变量中的字符串是否包含 所有 array 中的字符串。非常简单的事情,但我无法让它工作!我已经尝试了很多东西,但是太复杂了而且没有真正起作用。我正在寻找一些建议。
类似于:
var myArray = ["cat","dog","bird"];
var myString = "There is a cat looking the bird and a dog looking the cat";
var myString2 = "There is a cat and a dog looking one each other";
myArray 和 myString 必须为真并且 true 并且 myArray 和 myString2 必须为 假
我正在处理这样的事情:
var selector = "dir.class#id";
var code = '<div id="id" class="class"></div>';
var tokens = selector.split(/[\.,#]+/);
for (var i = tokens.length - 1; i >= 0; i--) {
var counter = [];
if (code.indexOf(tokens[0]) > -1 ) {
counter.concat(true);
}
}
谢谢!
你可以像下面那样做
var arePresentInText = function (text,words) {
return words.every(function(word){
return text.indexOf(word) > -1;
});
}
console.log(arePresentInText(myString,myArray)); //prints true
console.log(arePresentInText(myString2,myArray)); //prints false
这应该有效:
var myArray = ["cat","dog","bird", "cat"];
var myString = "There is a cat looking the bird and a dog looking the cat";
var myString2 = "There is a cat and a dog looking one each other";
function checkValue(arr, str){
var cnt = 0;
for(i in arr){
var val = arr[i];
if(str.indexOf(val) > -1) cnt++;
}
return (arr.length == cnt) ? true : false;
}
console.log(checkValue(myArray, myString));
function checkContainStr(arr, str){
for(i in arr){
if(str.indexOf(arr[i]) == -1)
return false;
}
return true;
}
试试这个功能:
function arrayElementsInString(ary, str){
for(var i=0,l=ary.length; i<l; i++){
if(str.indexOf(ary[i]) === -1){
return false;
}
}
return true;
}
如果你想匹配单词末尾的字符串,但不一定在开头,你需要使用正则表达式或其他处理。
检查 indexOf 匹配单词中任意位置的字符序列,而不是末尾。考虑以下与末尾或整个单词匹配的字符串:
var myArray = ["cat","dog","bird"];
var myString = "There is a cat looking the bird and a dog looking the cat";
var myString2 = "There is a cat and a dog looking one each other";
var myString3 = "There is a classcat and a iddog looking at bird";
var myString4 = "There is a catclass and a dog looking at bird";
document.write(myArray.every(function(word) {
return (new RegExp('\w*' + word + '\b').test(myString));
}) + '<br>');
document.write(myArray.every(function(word) {
return (new RegExp('\w*' + word + '\b').test(myString2));
}) + '<br>');
document.write(myArray.every(function(word) {
return (new RegExp('\w*' + word + '\b').test(myString3));
}) + '<br>');
document.write(myArray.every(function(word) {
return (new RegExp('\w*' + word + '\b').test(myString4));
}));
我必须检查变量中的字符串是否包含 所有 array 中的字符串。非常简单的事情,但我无法让它工作!我已经尝试了很多东西,但是太复杂了而且没有真正起作用。我正在寻找一些建议。
类似于:
var myArray = ["cat","dog","bird"];
var myString = "There is a cat looking the bird and a dog looking the cat";
var myString2 = "There is a cat and a dog looking one each other";
myArray 和 myString 必须为真并且 true 并且 myArray 和 myString2 必须为 假
我正在处理这样的事情:
var selector = "dir.class#id";
var code = '<div id="id" class="class"></div>';
var tokens = selector.split(/[\.,#]+/);
for (var i = tokens.length - 1; i >= 0; i--) {
var counter = [];
if (code.indexOf(tokens[0]) > -1 ) {
counter.concat(true);
}
}
谢谢!
你可以像下面那样做
var arePresentInText = function (text,words) {
return words.every(function(word){
return text.indexOf(word) > -1;
});
}
console.log(arePresentInText(myString,myArray)); //prints true
console.log(arePresentInText(myString2,myArray)); //prints false
这应该有效:
var myArray = ["cat","dog","bird", "cat"];
var myString = "There is a cat looking the bird and a dog looking the cat";
var myString2 = "There is a cat and a dog looking one each other";
function checkValue(arr, str){
var cnt = 0;
for(i in arr){
var val = arr[i];
if(str.indexOf(val) > -1) cnt++;
}
return (arr.length == cnt) ? true : false;
}
console.log(checkValue(myArray, myString));
function checkContainStr(arr, str){
for(i in arr){
if(str.indexOf(arr[i]) == -1)
return false;
}
return true;
}
试试这个功能:
function arrayElementsInString(ary, str){
for(var i=0,l=ary.length; i<l; i++){
if(str.indexOf(ary[i]) === -1){
return false;
}
}
return true;
}
如果你想匹配单词末尾的字符串,但不一定在开头,你需要使用正则表达式或其他处理。
检查 indexOf 匹配单词中任意位置的字符序列,而不是末尾。考虑以下与末尾或整个单词匹配的字符串:
var myArray = ["cat","dog","bird"];
var myString = "There is a cat looking the bird and a dog looking the cat";
var myString2 = "There is a cat and a dog looking one each other";
var myString3 = "There is a classcat and a iddog looking at bird";
var myString4 = "There is a catclass and a dog looking at bird";
document.write(myArray.every(function(word) {
return (new RegExp('\w*' + word + '\b').test(myString));
}) + '<br>');
document.write(myArray.every(function(word) {
return (new RegExp('\w*' + word + '\b').test(myString2));
}) + '<br>');
document.write(myArray.every(function(word) {
return (new RegExp('\w*' + word + '\b').test(myString3));
}) + '<br>');
document.write(myArray.every(function(word) {
return (new RegExp('\w*' + word + '\b').test(myString4));
}));