JSFiddle - React, Tailwind, and code Playground

by jfriend00

HTML

<div id="result"></div>

JavaScript

var test = [
    "HelloWorld",
    "hello World",
    "Hello World",
    " hello World"
];

function fixStr(str) {
    var out = str.replace(/^\s*[a-z]|[^\s][A-Z]/g, function(str, offset) {
        if (offset == 0) {
            return(str.toUpperCase());
        } else {
            return(str.substr(0,1) + " " + str.substr(1).toUpperCase());
        }
    });
    return(out);
}

for (var i = 0; i < test.length; i++) {
    $("#result").append("'" + fixStr(test[i]) + "'<br>");
}