Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert "camelCase" to "Camel Case"?

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.

like image 533
A Wizard Did It Avatar asked Nov 10 '10 21:11

A Wizard Did It


People also ask

How do you make a camel case?

CamelCase is a way to separate the words in a phrase by making the first letter of each word capitalized and not using spaces. It is commonly used in web URLs, programming and computer naming conventions. It is named after camels because the capital letters resemble the humps on a camel's back.

What is camel case example?

Meaning of camel case in English. the use of a capital letter to begin the second word in a compound name or phrase, when it is not separated from the first word by a space: Examples of camel case include "iPod" and "GaGa".

How do you make a Camelcase in Java?

Java follows the camel-case syntax for naming the classes, interfaces, methods, and variables. If the name is combined with two words, the second word will start with uppercase letter always, such as maxMarks( ), lastName, ClassTest, removing all the whitespaces.


2 Answers

"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>
like image 191
Vincent Robert Avatar answered Sep 30 '22 09:09

Vincent Robert


I had an idle interest in this, particularly in handling sequences of capitals, such as in xmlHTTPRequest. The listed functions would produce "Xml H T T P Request" or "Xml HTTPRequest", mine produces "Xml HTTP Request".

function unCamelCase (str){     return str         // insert a space between lower & upper         .replace(/([a-z])([A-Z])/g, '$1 $2')         // space before last upper in a sequence followed by lower         .replace(/\b([A-Z]+)([A-Z])([a-z])/, '$1 $2$3')         // uppercase the first character         .replace(/^./, function(str){ return str.toUpperCase(); }) } 

There's also a String.prototype version in a gist.

like image 44
Matt Wiebe Avatar answered Sep 30 '22 09:09

Matt Wiebe