查找字符,如果存在则递增 1
Look for characters and if exists increment 1
我正在尝试使用 javascript 创建一个文件夹,但在我这样做之前,我需要检查该文件夹是否存在以及它是否确实将数字增加 1。
这是我输入的名称:ABC Group
我希望 javascript 删除“The”并将其移至末尾,然后最后根据前 4 个字符后跟数字添加代码。如果目录中不存在前 4 个字符,那么它将从 01 开始并从那里递增。
这是我要输出的内容:
即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
}
我把你的代码放到一个函数中,每次函数被调用时我都在计算这个数字。
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));
我正在尝试使用 javascript 创建一个文件夹,但在我这样做之前,我需要检查该文件夹是否存在以及它是否确实将数字增加 1。
这是我输入的名称:ABC Group
我希望 javascript 删除“The”并将其移至末尾,然后最后根据前 4 个字符后跟数字添加代码。如果目录中不存在前 4 个字符,那么它将从 01 开始并从那里递增。
这是我要输出的内容:
即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
}
我把你的代码放到一个函数中,每次函数被调用时我都在计算这个数字。
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));