javascript - Convert enum (as string) from CAPS_CASE to camelCase -
i've found solution convert enum camelcase, it's not elegant.
var underscoretocamelcase = function(str) { str = (str === undefined || str === null) ? '' : str; str = str.replace(/_/g, " ").tolowercase(); return str.replace(/(?:^\w|[a-z]|\b\w|\s+)/g, function(match, index) { if (+match === 0) return ""; return index == 0 ? match.tolowercase() : match.touppercase(); }); }
so, if str my_enum_string return myenumstring.
there must way achieve single regex match?
try this:
var underscoretocamelcase = function(str) { return str.tolowercase() .replace(/_+(\w|$)/g, function ($$, $1) { return $1.touppercase(); }); }
Comments
Post a Comment