我有这个4任务类型的字符串:
ManagerTask
CoordinatorTask
BossTask
EmployTask
我需要一个方法/正则表达式来拆分/分离这些字符串:结果应该是:
Manager Task
Coordinator Task
Boss Task
Employ Task
谢谢!
答案 0 :(得分:2)
尝试以下方法:
function splitString(str){
return str.substring(0,str.lastIndexOf("T"))+" "+str.substring(str.lastIndexOf("T"));
}
console.log(splitString("ManagerTask"));

答案 1 :(得分:1)
var taskStrs = ['ManagerTask', 'CoordinatorTask', 'BossTask', 'EmployTask', "TaskMakerTask"];
function formatTaskName(task) {
var lastTaskInd = task.lastIndexOf("Task");
if(lastTaskInd == -1) {
return task;
}
return task.substring(0,lastTaskInd) + " " + task.substring(lastTaskInd);
}
for(var i = 0; i < taskStrs.length; i++) {
console.log(formatTaskName(taskStrs[i]));
}
答案 2 :(得分:1)
您可以使用Regex在任务和“任务”之前匹配任何内容,并在这些匹配组之间添加空格:
const modify = text => text.replace(/(.+)(Task)/, '$1 $2');
console.log(modify('ManagerTask'));
console.log(modify('CoordinatorTask'));
console.log(modify('BossTask'));
console.log(modify('EmployTask'));
此外,如果您需要此问题的一般解决方案,您可以使用:
const modify = text => text
// Find all capital letters and add space before them
.replace(/([A-Z])/g, ' $1')
// Remove the first space - otherwise result would be for example ' OfficeManagerTask'
.substring(1);
console.log(modify('OfficeManagerTask'));
console.log(modify('AngryBossTask'));
console.log(modify('ManagerTask'));
console.log(modify('CoordinatorTask'));
console.log(modify('BossTask'));
console.log(modify('EmployTask'));