Skip to content

Instantly share code, notes, and snippets.

@vjt
Created February 15, 2011 15:50
Show Gist options
  • Save vjt/827679 to your computer and use it in GitHub Desktop.
Save vjt/827679 to your computer and use it in GitHub Desktop.
String.camelize
// I thought I needed it, but I didn't need it anymore,
// but I already implemented it. So, here we go, if you
// ever would need a Javascript camelize implementation
// you can freely use this :-).
// - vjt@openssl.it Tue Feb 15 16:49:52 CET 2011
jQuery.extend (String.prototype, {
camelize: function () {
return this.replace (/(?:^|[-_])(\w)/g, function (_, c) {
return c ? c.toUpperCase () : '';
})
}
});
@sebastianconcept
Copy link

golden! thanks

@knutole
Copy link

knutole commented Jun 6, 2014

thanks!

vanilla version:

String.prototype.camelize = function () {
return this.replace (/(?:^|[-])(\w)/g, function (, c) {
return c ? c.toUpperCase () : '';
});
}

@mishrsud
Copy link

@knutole: modifying the prototype of a built-in type is considered bad practice in JavaScript...

@pelkoju
Copy link

pelkoju commented Aug 22, 2014

Thanks!

@algesten
Copy link

@mishrud get over it

@Kosmin
Copy link

Kosmin commented May 7, 2015

👍 thanks

@viniciusCamargo
Copy link

More ES6ish:

const camelCase = (string) => {
/*
 * by http://stackoverflow.com/users/140811/scott
 * at http://stackoverflow.com/a/2970588/6776673
 */
  return string
    .replace(/\s(.)/g, $1 => $1.toUpperCase())
    .replace(/\s/g, '')
    .replace(/^(.)/, $1 => $1.toLowerCase())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment