Javascript String camelize()
String.prototype.camelize = function() { var parts = this.split('-'), len = parts.length; if (len == 1) return parts[0]; var camelized = this.charAt(0) == '-' ? parts[0].charAt(0).toUpperCase() + parts[0].substring(1) : parts[0];/*from w w w . ja va2s.com*/ for (var i = 1; i < len; i++) camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1); return camelized; };
'use strict';//w ww . ja va2s . co m // TODO: Strip all but [a-zA-Z] String.prototype.camelize = function () { return this.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function (match, index) { if (+match === 0) { return ''; }// or if (/\s+/.test(match)) for white spaces return index === 0 ? match.toLowerCase() : match.toUpperCase(); }); };
// Extend String prototype String.prototype.camelize = function camelize(){ return this.replace (/(?:^|[-_])(\w)/g, function (_, c) { return c ? c.toUpperCase () : ''; });/*from w ww.j a v a 2 s. co m*/ }; String.random = function random(length){ var result = ''; for(var i = 0; i < length / 10; i++){ result += Math.random().toString(36).substr(2).substr(0, 10); } return result.substr(0, length); };
/**// ww w.ja v a 2 s. c o m * String#camelize() -> String * * Converts a string separated by dashes into a camelCase equivalent. For * instance, `'foo-bar'` would be converted to `'fooBar'`. * * Prototype uses this internally for translating CSS properties into their * DOM `style` property equivalents. * * ##### Examples * * 'background-color'.camelize(); * // -> 'backgroundColor' * * '-moz-binding'.camelize(); * // -> 'MozBinding' **/ String.prototype.camelize = function() { return this.replace(/(?:^|_+)(.)?/g, function(match, chr) { return chr ? chr.toUpperCase() : ''; }); };