为了账号安全,请及时绑定邮箱和手机立即绑定

如果最后一个单词是数字并增加数字 - JS,则删除字符串的最后一个单词

如果最后一个单词是数字并增加数字 - JS,则删除字符串的最后一个单词

牛魔王的故事 2023-03-03 09:34:01
我有一个字符串,如果最后一个单词是数字并增加数字,我需要删除字符串的最后一个单词。如果最后一个单词不是数字,我们会一直检查直到出现相同的单词,然后将计数与单词一起抛出。这是Count string occurrence 和 replace with string and count的扩展- JS - 后半部分正在运行。无法使第一个示例正常工作。请指教。例子:Model transfer 2 transfer - Model transfer 3 //预期输出模型迁移 transfer Transfer - 模型迁移 3 //预期输出Model transfertransfer Transfer Transfer - Model transfer 4 //预期输出Model transfer - Model transfer //预期输出模型 - 模型 //预期输出var name = "Model Transfer 2 transfer";const name1 = 'Model transfer model transfer';const name2 = 'Model transfer transfer transfer';const name3 = 'Model Transfer transfer Transfer';const name4 = 'Model transfer transfer transfer';const renameNameOccurrence = (  name,  occurrenceName) => {  var regExp = new RegExp(`(?<=${occurrenceName}\\s)(.+?)(?=\\s+${occurrenceName})`, "ig");  var results = Number(name.match(regExp)[0]);  if (typeof results == 'number') {    const abc = name.replace(/\w+[.!?]?$/, '');  }  const nameSplit = name.split(" ");  const occurrenceCount = (    name.match(new RegExp(`${occurrenceName}`, "gi")) || []  ).length;  let modifiedName = "";  if (occurrenceCount > 1) {    for (let i = 0; i < nameSplit.length - 1; i++) {      if (nameSplit[i].toLowerCase() === nameSplit[i + 1].toLowerCase()) {        if (occurrenceCount > 1) {          modifiedName = name            .replace(new RegExp(`\\s*${occurrenceName}\\s*`, "gi"), "")            .concat(` ${occurrenceName.toLowerCase()} ${occurrenceCount}`);        } else {          modifiedName = name;        }      } else {        modifiedName = name;      }    }  } else {    modifiedName = name;  }  return modifiedName;};
查看完整描述

1 回答

?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

该解决方案依赖于按单词拆分和计算transfer. 它还会检测最后一个单词之前是否有整数,如果是,则按以下代码所述处理这种情况:


var name = "Model Transfer 2 transfer";

const name1 = 'Model transfer model transfer';

const name2 = 'Model transfer transfer transfer';

const name3 = 'Model Transfer transfer Transfer';

const name4 = 'Model transfer transfer transfer';


const renameNameOccurrence = (name, occurrenceName) => {

    

    // split by words as lowercased

    let s = name.toLowerCase().split(" ")  

    

    // Now count the 'transfer' words

    let t = 0;

    for (var i = 0; i < s.length; i++) {

        

        if (s[i] == "transfer") {

            t++;

        }

    }

    

    // Now see if a integer precedes last word

    

    w = s.length - 2; // -2 since length is base 1, and we need word before last

    if (w > 1) {  // excluding the assumed 1st word Model

    

        if ( parseInt( s[w] ).toString() == s[w]) {

            t++; // increment transfer words, since count is consistent

            console.log("hello")

        }

    }

    

    // Now recompose the string

    

    

    

    if (t > 1) {

        

        return name.split(" ")[0] + " Transfer " + t

    

    }

    else if (t > 0) {

        return name.split(" ")[0] + " Transfer"

    }

    else {

        return name.split(" ")[0]

    }

    

    

}


/*


Model transfer 2 transfer - Model transfer 3 //Expected output

Model transfer transfer Transfer - Model transfer 3 //Expected output

Model transfertransfer Transfer Transfer - Model transfer 4 //Expected output

Model transfer - Model transfer //Expected output

Model - Model //Expected output


*/


console.log( renameNameOccurrence( "Model transfer 2 transfer" ))

console.log( renameNameOccurrence( "Model transfer transfer Transfer" ))

console.log( renameNameOccurrence( "Model transfer transfer Transfer Transfer" ))

console.log( renameNameOccurrence( "Model transfer" ))

console.log( renameNameOccurrence( "Model" ))


查看完整回答
反对 回复 2023-03-03
  • 1 回答
  • 0 关注
  • 97 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信