JSFiddle - React, Tailwind, and code Playground

HTML

<p>
    Input: <code id="input">fOO BaR_baZ</code>
</p>

<p>
    Output: <code id="output"></code>
</p>

CSS

code { background-color:#eee; }

JavaScript

function foo(str) {
    return $(str.split(/\s|_/)).map(function() {
        return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
    }).get().join("");
}

$("#output").text( foo( $("#input").text() ) );