简体   繁体   English

如何拆分camelCase字符串并检查每个拆分字是否是数组的一部分?

[英]How to split a camelCase string and check if each split word is part of an array or not?

Assuming I have an array of words and a few camelCase strings as follows: 假设我有一个单词数组和一些camelCase字符串 ,如下所示:

var arr = ["hello", "have", "a", "good", "day", "stackoverflow"];
var str1 = "whenTheDayAndNightCollides";
var str2 = "HaveAGoodDay";
var str3 = "itIsAwfullyColdDayToday";
var str4 = "HelloStackoverflow";

How would I split the camelCase words into individual strings, compare each split string (converted to lowercase) to the arr array elements and return true if every split string is part of the specified array? 如何将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

As suggested in this other SO thread , I tried to use the every() method and the indexOf() method to test if every split string can be found in the array or not as seen in the following Code Snippet but it's not working: 正如在其他SO线程中所建议的那样,我尝试使用every()方法和indexOf()方法来测试是否可以在数组中找到每个拆分字符串,如下面的代码片段所示,但它不起作用:

 var arr = ["hello", "have", "a", "good", "day", "stackoverflow"]; function checkString(wordArray, str) { // split the camelCase words var x = str.replace(/([AZ])/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")); 

What am I doing wrong? 我究竟做错了什么?

You have to convert your split strings to lowercase before doing the comparison. 在进行比较之前,您必须将拆分字符串转换为小写。

Also, you have to remove the empty string at the beginning of the list which is inserted by replace when the first letter of your string is uppercase. 此外,您必须删除列表开头的空字符串,当字符串的第一个字母为大写时,由replace插入。

You can use includes instead of indexOf . 您可以使用includes而不是indexOf

 var arr = ["hello", "have", "a", "good", "day", "stackoverflow"]; function checkString(wordArray, str) { return str.replace(/([AZ])/g, ' $1') .split(" ") // split on spaces .map(s => s.toLowerCase()) .filter(s => s) .every(e => wordArray.includes(e)); } console.log("should return true -> " + checkString(arr, "HelloStackoverflow")); console.log("should return false -> " + checkString(arr, "itIsAwfullyColdDayToday")); 

you were very close, but there was 2 problems: 你非常接近,但有2个问题:

  • one of them was on your str.replace , it was returning something like ["", "Hello", "Stackoverflow"] when you had a uppercase letter on the start of the string. 其中一个在你的str.replace ,当你在字符串的开头有一个大写字母时,它返回了类似["", "Hello", "Stackoverflow"]的东西。

  • the second one was on your comparison wordArray.indexOf() it is case sensitive, so you needed to add toLowerCase() so it ends up like this: wordArray.indexOf(e.toLowerCase()) 第二个是你的比较wordArray.indexOf()它是区分大小写的,所以你需要添加toLowerCase()所以它最终会像这样: wordArray.indexOf(e.toLowerCase())

 var arr = ["hello", "have", "a", "good", "day", "stackoverflow"]; function checkString(wordArray, str) { var x = str.replace(/([AZ])/g, ' $1').split(" "); // split the camelCase words //filter spaces var filtered = x.filter(s => s != ''); return filtered.every(e => { return wordArray.indexOf(e.toLowerCase()) >= 0; }); } console.log("should return true ->" + checkString(arr, "HelloStackoverflow")); console.log("should return false ->" + checkString(arr, "itIsAwfullyColdDayToday")); 

A few problems: first, all the arr values start with lowercase letters, but your camelCase tests (of course) contain caps. 一些问题:首先,所有的arr值都以小写字母开头,但是你的camelCase测试(当然)包含大写字母。 For that reason I've replaced 出于这个原因,我已经取代了

wordArray.indexOf(e)

with

wordArray.indexOf(e.toLowerCase())

Second, since your first test case starts with an uppercase letter, your regex is prepending a space, which then gets split into its own "word". 其次,由于您的第一个测试用例以大写字母开头,因此您的正则表达式会在前面添加一个空格,然后将其split为自己的“单词”。 To deal with this I've just added !e || 为了解决这个问题,我刚刚添加了!e || to the every condition, so it will always return true for empty strings generated by leading caps. 对于every条件,所以对于由前导上限生成的空字符串,它总是返回true。

 var arr = ["hello", "have", "a", "good", "day", "stackoverflow"]; function checkString(wordArray, str) { var x = str.replace(/([AZ])/g, ' $1').split(" "); // split the camelCase words return x.every(function (e) { return !e || wordArray.indexOf(e.toLowerCase()) >= 0; }); } console.log("should return true ->" + checkString(arr, "HelloStackoverflow")); console.log("should return false ->" + checkString(arr, "itIsAwfullyColdDayToday")); 

This is a pretty simple version. 这是一个非常简单的版本。

 const checkString = arr => str => str .split(/(?=[^az])/) .every(s => arr.includes(s.toLowerCase())) const arr = ["hello", "have", "a", "good", "day", "stackoverflow"] console.log(checkString(arr)('whenTheDayAndNightCollides')) console.log(checkString(arr)('HaveAGoodDay')) console.log(checkString(arr)('itIsAwfullyColdDayToday')) console.log(checkString(arr)('HelloStackoverflow')) 

Of course you could also name the intermediate function: 当然你也可以命名中间函数:

const correct = checkString(arr)
correct('HaveAStackoverflowDay') //=> true

For this particular case, I will use a lookahead assertion (?=...) , which is a non-capturing construct and I will use it directly with the String::split() method. 对于这种特殊情况,我将使用一个前瞻断言 (?=...) ,这是一个非捕获构造,我将直接使用它与String :: split()方法。 This will solve the problem of the extra generated empty string element on your array when the string begins with an uppercase letter. 当字符串以大写字母开头时,这将解决数组上额外生成的empty string元素的问题。 And also I will give a try to Array::includes() in exchange of indexOf() 而且我将尝试使用Array :: includes()来交换indexOf()

 var arr = ["hello", "have", "a", "good", "day", "stackoverflow"]; function checkString(wordArray, str) { return str.split(/(?=[AZ])/g).every( e => wordArray.includes(e.toLowerCase()) ); } console.log("should return true ->" + checkString(arr, "HelloStackoverflow")); console.log("should return false ->" + checkString(arr, "itIsAwfullyColdDayToday")); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM