How to convert "camelCase" to "Camel Case"?
javascript, regex
Solution
"thisStringIsGood"
// insert a space before all caps
.replace(/([A-Z])/g, ' $1')
// uppercase the first character
.replace(/^./, function(str){ return str.toUpperCase(); })
displays
This String Is Good
(function() {
const textbox = document.querySelector('#textbox')
const result = document.querySelector('#result')
function split() {
result.innerText = textbox.value
// insert a space before all caps
.replace(/([A-Z])/g, ' $1')
// uppercase the first character
.replace(/^./, (str) => str.toUpperCase())
};
textbox.addEventListener('input', split);
split();
}());
#result {
margin-top: 1em;
padding: .5em;
background: #eee;
white-space: pre;
}
<div>
Text to split
<input id="textbox" value="thisStringIsGood" />
</div>
<div id="result"></div>
Problem
I’ve been trying to get a JavaScript regex command to turn something like `"thisString"` into `"This String"` but the closest I’ve gotten is replacing a letter, resulting in something like `"Thi String"` or `"This tring"`. Any ideas? To clarify I can handle the simplicity of capitalizing a letter, I’m just not as strong with RegEx, and splitting `"somethingLikeThis"` into `"something Like This"` is where I’m having trouble.