I have this 4 task type of string:
ManagerTask
CoordinatorTask
BossTask
EmployTask
I need a method/regexp to split/separate these strings: The result should be:
Manager Task
Coordinator Task
Boss Task
Employ Task
Thank you!
Try the following:
function splitString(str){
return str.substring(0,str.lastIndexOf("T"))+" "+str.substring(str.lastIndexOf("T"));
}
console.log(splitString("ManagerTask"));
You can use Regex to match anything before task and the 'Task' and add space between these to matched groups:
const modify = text => text.replace(/(.+)(Task)/, '$1 $2');
console.log(modify('ManagerTask'));
console.log(modify('CoordinatorTask'));
console.log(modify('BossTask'));
console.log(modify('EmployTask'));
Also if you needed general solution for this issue you can use:
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'));
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]));
}