The very last version:
"thisIsNotAPain"
.replace(/^[a-z]|[A-Z]/g, function(v, i) {
return i === 0 ? v.toUpperCase() : " " + v.toLowerCase();
}); // "This is not a pain"
The old solution:
"thisIsAPain"
.match(/^(?:[^A-Z]+)|[A-Z](?:[^A-Z]*)+/g)
.join(" ")
.toLowerCase()
.replace(/^[a-z]/, function(v) {
return v.toUpperCase();
}); // "This is a pain"
console.log(
"thisIsNotAPain"
.replace(/^[a-z]|[A-Z]/g, function(v, i) {
return i === 0 ? v.toUpperCase() : " " + v.toLowerCase();
}) // "This is not a pain"
);
console.log(
"thisIsAPain"
.match(/^(?:[^A-Z]+)|[A-Z](?:[^A-Z]*)+/g)
.join(" ")
.toLowerCase()
.replace(/^[a-z]/, function(v) {
return v.toUpperCase();
}) // "This is a pain"
);