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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -