如果子字符串是数组的一部分,请替换该子字符串

时间:2019-08-07 16:41:03

标签: javascript python

我已经用Python编写了一些代码,但是我必须用JavaScript编写(对于Google Apps脚本)。我会说我对JS很不好,似乎无法复制我在Python中所做的事情。 基本上,我有一个字符串,其中可能有“ /”或“-”,如果有,我希望它用“”替换与2个特定数组匹配的所有子字符串。

(对于上下文,whitelistdias和whitelistmes是两个数组,其中包含多个单词)

 var whitelistdias = ["terça-feira", "quarta-feira",
                     "quinta-feira", "sexta-feira", "sábado", "domingo",
                     "segunda", "terça", "quarta", "quinta", "sexta", "sabado",
                     "terca"];

  var whitelistmes = ["janeiro", "fevereiro", "março", "abril", "maio", "junho",
                    "julho", "agosto", "setembro", "outubro", "novembro",
                    "dezembro", "jan", "fev", "mar", "abr", "mai", "jun",
                    "jul", "ago", "set", "out", "nov", "dez"];

我为此使用的Python代码发布在下面:

    idk = str(idk)
    if "/" in idk or "-" in idk:
        for i in whitelistmes:
            if i in idk:
                mes = i
                idk = idk.replace(i, "")

        for i in whitelistdias:
            if i in idk:
                idk = str(idk)
                idk = idk.replace(i, "")

示例:

基本上,我们假设字符串为“星期一,10月2日,23:59”。我想测试字符串是否具有“ /”或“-”,如果是,则将“ Monday”和“ October”替换为“”。最终结果将是“,2nd 23:59”

抱歉,这似乎微不足道,但我确实找不到解决办法,并且一直在寻找类似的解决方案,但无济于事。

3 个答案:

答案 0 :(得分:3)

首先,我们使用string.protoype.includes检查字符串是否包含“ /”或“-”,然后创建一个包含要替换内容的数组。对于该数组中的每个数组,一一替换字符串。

let str = "Monday, 2nd October, 23:59 -" // it contains "-"
let thingsToReplace = ["Monday", "October"]
if (str.includes("/") || str.includes("-")) { // || this means that if any of them is true then returns true
  thingsToReplace.forEach((strs) => { // for each the array and replace every string
    str = str.replace(strs, ""); // replace the string via string.replace(what to replace, and with what)
  })
}
console.log(str) // log it so we can see

答案 1 :(得分:2)

如果有人对正则表达式的更短解决方案感兴趣:

let idk = "Monday, 2nd - October, 23:59";
let whitelist = ["Monday", "October"];
let regstr = new RegExp(whitelist.join('|'),'g');

// just to show the replace code and ignore '/' and '-' check
idk = idk.replace(regstr,'');

console.log(idk);

答案 2 :(得分:1)

事物的结合:

  • 对于字符串中存在子字符串,可以使用indexOf()includes()
  • 对于迭代,请使用for loopforEach
  • 要替换的功能是相同的:replace()

这是运行代码:

idk = "Monday, 2nd - October, 23:59";
whitelistmes = ["January", "October"];
whitelistdias = ["Tuesday", "Monday"];
if (idk.indexOf("/") != -1 || idk.indexOf("-") != -1) {
    for (i in whitelistmes) {
        if (idk.indexOf(whitelistmes[i]) != -1) {
            //mes = i
            idk = idk.replace(whitelistmes[i], "");
        }
    }

    for (i in whitelistdias) {
        if (idk.indexOf(whitelistdias[i]) != -1) {
            //idk = str(idk)
            idk = idk.replace(whitelistdias[i], "");
        }
    }
}
console.log(idk);

以上版本复制了您的代码。这是更好的版本:

idk = "Monday, 2nd - October, 23:59";
whitelistmes = ["January", "October"];
whitelistdias = ["Tuesday", "Monday"];
if (idk.includes("/") || idk.includes("-")) {
    [...whitelistmes].forEach(token => {
        if (idk.includes(token)) {
            idk = idk.replace(token, "");
        }
    });

    [...whitelistdias].forEach(token => {
        if (idk.includes(token)) {
            idk = idk.replace(token, "");
        }
    });
}
console.log(idk);

请注意,您也可以将whitelistmeswhitelistdias合并为一个,因为两者的操作相同。