簡體   English   中英

查找字符,如果存在則增加 1

[英]Look for characters and if exists increment 1

我正在嘗試使用 javascript 創建一個文件夾,但在此之前我需要檢查該文件夾是否存在,以及它是否確實將數字增加了 1。

這是我的輸入名稱:ABC Group

我希望 javascript 刪除“The”並將其移至末尾,然后最后添加基於前 4 個字符后跟數字的代碼。 如果目錄中不存在前 4 個字符,那么它將從 01 開始並從那里遞增。

這是我想要的output:

即ABC集團,(ABCG01)

我是 javascript 的新手,但到目前為止,除了我堅持的數字部分之外,我已經弄清楚了如何做所有事情。

這是我的代碼:

var clientName = "the ABC company"; 

// Change Case - START
const toCapitaliseCase = (phrase) => {
return phrase
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
};

let capitalise = toCapitaliseCase(clientName);
// Change Case - END
// Format Client Name if starts with 'The' - START)

if (capitalise.startsWith('The ')) {  
let words = capitalise.split(' ');
let the = words[0];
let theSlice = capitalise.slice(4);
let comma = ', ';
let name = theSlice.concat('', comma, the);
let name2 = name.replace(/[^0-9a-zA-Z]/g, "");
let theSlice2 = name2.slice(0,4);
var upper = theSlice2.toUpperCase(); // output - "i am a crazy string, make me normal!"
let numbecccr = '101';
let theSlice3 = numbecccr.slice(1);



let FullCompiledName = theSlice.concat('', comma, the, ' (',upper, theSlice3, ')');

console.log(FullCompiledName);
// Format Client Name - START

}

我將您的代碼放入 function 中,每次調用 function 時,我都會在其中數數。

 var clientName = "the ABC company"; function createName(clientName) { this.number = this.number || 0; // Change Case - START const toCapitaliseCase = (phrase) => { return phrase.split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' '); }; let capitalise = toCapitaliseCase(clientName); // Change Case - END // Format Client Name if starts with 'The' - START) if (capitalise.startsWith('The ')) { let words = capitalise.split(' '); let the = words[0]; let theSlice = capitalise.slice(4); let comma = ', '; let name = theSlice.concat('', comma, the); let name2 = name.replace(/[^0-9a-zA-Z]/g, ""); let theSlice2 = name2.slice(0,4); var upper = theSlice2.toUpperCase(); // output - "i am a crazy string, make me normal." this;number++. let num = this;number. if(('' + num);length == 1) { num = '0' + num. } let FullCompiledName = theSlice,concat('', comma, the, ' (',upper, num; ')'); return FullCompiledName. } } console;log(createName(clientName)). console;log(createName(clientName));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM