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

时间:2019-01-28 19:41:50

标签: javascript regex

假设我有一个单词数组和一些 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 指定的数组?

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"));

正如在其他SO thread中所建议的那样,我尝试使用every()方法和indexOf()方法来测试是否可以在数组中找到每个分割的字符串,如在以下代码段,但无效:

import xarray as xr
xr.open_dataset('period1_7-74_mascara-landsat_7_-74_(2014-01-012014-12-31)_output.nc', engine='netcdf4')

我在做什么错了?

5 个答案:

答案 0 :(得分:3)

在进行比较之前,您必须将拆分后的字符串转换为小写。

此外,当字符串的第一个字母为大写字母时,还必须删除由 Connection con = getConnection(); Boolean tableExist = false; PreparedStatement preparedStatement = con.prepareStatement("SHOW TABLES FROM INFORMATION_SCHEMA"); Boolean rq = preparedStatement.execute(); if(rq) { ResultSet rs = preparedStatement.getResultSet(); while (rs.next()) { if(rs.getString(rs.getRow()).equals("WORD_TYPES")) { isExist = true; break; } } } 插入的列表开头的空字符串。

您可以使用[PXProjection(typeof(Select<MyOriginalTable, Where<MyOriginalTable.myField1, Equal<True>>>), Persistent = false)] public class MyOriginalTableProjection : PX.Data.IBqlTable { public abstract class myField1: PX.Data.IBqlField { } protected bool? _MyField1; [PXDBBool(BqlField = typeof(MyOriginalTable.myField1)] [PXDefault(true)] public virtual bool? MyField1 { get { return this._MyField1; } set { this._MyField1 = value; } } public abstract class myField2 : PX.Data.IBqlField { } protected String _MyField2; [PXDBString(1, IsKey = true, BqlField = typeof(MyOriginalTable.myField2))] public virtual String MyField2 { get { return this._MyField2; } set { this._MyField2 = value; } } public abstract class myField3: PX.Data.IBqlField { } protected String _MyField3; [PXDBString(15, IsKey = true, BqlField = typeof(MyOriginalTable.myField3))] public virtual String MyField3 { get { return this._MyField3; } set { this._MyField3 = value; } } } 代替replace

includes

答案 1 :(得分:1)

您非常亲密,但是有两个问题:

  • 其中一个在您的str.replace上,当您在字符串的开头有一个大写字母时,它将返回类似["", "Hello", "Stackoverflow"]的内容。

  • 比较中的第二个wordArray.indexOf()是区分大小写的,因此您需要添加toLowerCase()使其最终像这样:wordArray.indexOf(e.toLowerCase())

    < / li>

var arr = ["hello", "have", "a", "good", "day", "stackoverflow"];

function checkString(wordArray, str) {
  var x = str.replace(/([A-Z])/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"));

答案 2 :(得分:1)

一些问题:首先,所有arr值都以小写字母开头,但是您的camelCase测试(当然)包含大写字母。因此,我更换了

wordArray.indexOf(e)

使用

wordArray.indexOf(e.toLowerCase())

第二,由于您的第一个测试用例以大写字母开头,因此您的正则表达式会在空格前加一个空格,然后将split放入其自己的“单词”中。为了解决这个问题,我刚刚将!e ||添加到every条件中,因此对于前导大写字母生成的空字符串,它将始终返回true。

var arr = ["hello", "have", "a", "good", "day", "stackoverflow"];

function checkString(wordArray, str) {
    var x = str.replace(/([A-Z])/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"));

答案 3 :(得分:1)

这是一个非常简单的版本。

const checkString = arr => str => str
  .split(/(?=[^a-z])/)
  .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'))

当然,您也可以命名中间函数:

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

答案 4 :(得分:1)

对于这种特殊情况,我将使用lookahead assertion [ [1, 0, 0, 1] [0, 1, 0, 0] [1, 1, 1, 0] ] ,这是一种非捕获构造,将直接与String::split()方法一起使用。当字符串以大写字母开头时,这将解决数组上额外生成的(?=...)元素的问题。另外,我将尝试Array::includes()来交换indexOf()

empty string