Предполагая, что у меня есть массив слов и несколько строк camelCase следующим образом:
var arr = ["hello", "have", "a", "good", "day", "stackoverflow"];
var str1 = "whenTheDayAndNightCollides";
var str2 = "HaveAGoodDay";
var str3 = "itIsAwfullyColdDayToday";
var str4 = "HelloStackoverflow";
Как бы я разбил слова camelCase
на отдельныестрок, сравнить каждую строку разбиения (преобразованную в нижний регистр) с элементами массива arr
и вернуть true
, если каждая строка разбиения является частью указанного массива?
"whenTheDayAndNightCollides" // should return false since only the word "day" is in the array
"HaveAGoodDay" // should return true since all the words "Have", "A", "Good", "Day" are in the array
"itIsAwfullyColdDayToday" // should return false since only the word "day" is in the array
"HelloStackoverflow" // should return true since both words "Hello" and "Stackoverflow" are in the array
Как предлагается в этомдругой SO thread , я пытался использовать метод every () и метод indexOf () , чтобы проверить, можно ли найти каждую строку разбиения в массиве илине так, как показано в следующем фрагменте кода , но он не работает:
var arr = ["hello", "have", "a", "good", "day", "stackoverflow"];
function checkString(wordArray, str)
{
// split the camelCase words
var x = str.replace(/([A-Z])/g, ' $1').split(" ");
return x.every(e => {
return wordArray.indexOf(e.toLowerCase()) >= 0;
});
}
console.log("should return true ->" + checkString(arr, "HelloStackoverflow"));
console.log("should return false ->" + checkString(arr, "itIsAwfullyColdDayToday"));
Что я делаю не так?