3

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"

TaylorMac
  • 8,882
  • 21
  • 76
  • 104

6 Answers6

9

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
  • 110,530
  • 99
  • 389
  • 494
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Nice, that's what I was looking for and @lpg has the same solution, I'll upvote. I didn't want to have an unnecessary conditional, thanks for this regex change. – TaylorMac Jul 23 '14 at 17:11
5

Maybe you could use this:

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

(taken from here)

Community
  • 1
  • 1
lpg
  • 4,897
  • 1
  • 16
  • 16
1

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
anubhava
  • 761,203
  • 64
  • 569
  • 643
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".

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
0

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

Austin Brunkhorst
  • 20,704
  • 6
  • 47
  • 61
  • And you will get the error `"TypeError: Cannot read property 'toUpperCase' of undefined"` if there happens to be double spaces. [I know it is not in the test case above] – epascarello Jul 23 '14 at 16:57
  • 1
    Why replace and not just split on one or more spaces? `.split(/\s+/)` – epascarello Jul 23 '14 at 17:01
0

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"
Cyril N.
  • 38,875
  • 36
  • 142
  • 243