Convert sentence to camel case javascript

Having a difficult time finding a solution to this, there are several solutions for the reverse.

I have considered replacing every " " and the following first character with an uppercase version of itself:

value.toLowerCase().replace(/\s+/g, function (g) { return g[1].toUpperCase() })

Only, the regex /\s+/g needs to be changed to match the first character.

If there is an existing question that is exactly this, provide link and I will close this myself. I can't find a solution on SO


Examples:

"I walk my dog to the park" or "i Walk my DOG to the Park" => "iWalkMyDogToThePark"

asked Jul 23, 2014 at 16:34

TaylorMacTaylorMac

8,69221 gold badges75 silver badges104 bronze badges

2

You need to catch the next character. You can use (.) or ([a-z])

var toCamelCase = function(string){
  return string.replace(/\s+(.)/g, function (match, group) { 
    return group.toUpperCase()  
  })
}

mikemaccana

98.1k89 gold badges357 silver badges447 bronze badges

answered Jul 23, 2014 at 16:45

Convert sentence to camel case javascript

epascarelloepascarello

198k20 gold badges186 silver badges226 bronze badges

1

Maybe you could use this:

function camelCase(value) { 
    return value.toLowerCase().replace(/\s+(.)/g, function(match, group1) {
        return group1.toUpperCase();
    });
}

(taken from here)

answered Jul 23, 2014 at 16:44

lpglpg

4,8491 gold badge15 silver badges15 bronze badges

You can use this camel case conversion code:

function toCamelCase(str) {
  return str.replace(/(?:^.|[A-Z]|\b.)/g, function(letter, index) {
    return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
  }).replace(/\s+/g, '');
}

var val = toCamelCase("Sentence case");
//=> sentenceCase

val = toCamelCase('hello how are you');
//=> helloHowAreYou

answered Jul 23, 2014 at 16:47

anubhavaanubhava

728k61 gold badges529 silver badges609 bronze badges

0

Are you looking for something like this:

var string = "Hello there what are You doing yes";
string.replace(/([A-Z])([a-z]+)\s+([a-z])([a-z]+)/g, function($1, $2, $3, $4, $5) { 
    return $2.toLowerCase() + $3 + $4.toUpperCase() + $5; 
});

This prints out "helloThere what are youDoing yes".

answered Jul 23, 2014 at 16:40

Vivin PaliathVivin Paliath

91.9k39 gold badges216 silver badges293 bronze badges

I think a simpler solution can be achieved without Regex. Just split the string from spaces, and join them with the appropriate casing.

var value = '...';

var camelCase = value.split(' ').map(function(word, i) {
    return (word[0] || '')[i == 0 ? 'toLowerCase' : 'toUpperCase']() +
           word.substr(1).toLowerCase();
}).join('');

JSFiddle

answered Jul 23, 2014 at 16:54

2

I ended up doing the following (ES6):

function camelCase (str) {
    return str.split(/[^a-zA-Z0-9]/g).map((x, index) => {
        if (index === 0) return x.toLowerCase()
        return x.substr(0, 1).toUpperCase() + x.substr(1).toLowerCase()
    }).join('')
}

It takes the assumption that in general, the aim to convert to camelCase is for variables, so no accents or odd chars should be present (or will be split).

camelCase("I walk my dog to the park")
> "iWalkMyDogToThePark"

answered Mar 26, 2021 at 7:58

Cyril N.Cyril N.

37.8k34 gold badges130 silver badges232 bronze badges

Not the answer you're looking for? Browse other questions tagged javascript regex or ask your own question.

How do you make a camelCase string in JavaScript?

JavaScript Algorithm: Convert String to Camel Case.
let newStr = "";.
if (str) { // blah blah. } else { return newStr. }.
if (str) { let wordArr = str.split(/[-_]/g); } else { return newStr. }.
for (let i in wordArr) { if (i > 0) { newStr += wordArr[i].charAt(0).toUpperCase() + wordArr[i].slice(1); } else { ... .
return newStr;.

How do you use camel case in JavaScript?

camelCase is used by JavaScript itself, by jQuery, and other JavaScript libraries. Do not start names with a $ sign..
Variable and function names written as camelCase..
Global variables written in UPPERCASE (We don't, but it's quite common).
Constants (like PI) written in UPPERCASE..

What is camelCase 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?

There are two ways of using Camel case: Example, firstName, lastName, actionEvent, printArray( ), etc. The Upper camel case also known as the title case, where the first character of the first word is in uppercase. This convention is usually followed while naming the classes and interfaces.