Convert hyphens to camel case (camelCase)

后端 未结 13 1882
梦谈多话
梦谈多话 2020-12-04 08:02

With regex (i assume) or some other method, how can i convert things like:

marker-image or my-example-setting to markerImage o

13条回答
  •  被撕碎了的回忆
    2020-12-04 08:08

    Here is another option that combines a couple answers here and makes it method on a string:

    if (typeof String.prototype.toCamel !== 'function') {
      String.prototype.toCamel = function(){
        return this.replace(/[-_]([a-z])/g, function (g) { return g[1].toUpperCase(); })
      };
    }
    

    Used like this:

    'quick_brown'.toCamel(); // quickBrown
    'quick-brown'.toCamel(); // quickBrown
    

提交回复
热议问题