JavaScript RegExp to CamelCase a hyphened CSS property

后端 未结 6 1199
心在旅途
心在旅途 2021-02-19 12:47

I am trying to change CSS properties like this one.

-moz-border-radius

To the JavaScript CSS property like so.

MozBorderRadius
         


        
6条回答
  •  生来不讨喜
    2021-02-19 13:48

    Additional Information...

    MozBorderRadius = PascalCase A.K.A UpperCamelCase.

    mozBorderRadius = camelCase.

    moz_border_radius = snake_case.

    var string = "hyphen-delimited-to-camel-case"
    or
    var string = "snake_case_to_camel_case"
    
    
    function toCamelCase( string ){
      return string.toLowerCase().replace(/(_|-)([a-z])/g, toUpperCase );
    }
    
    function toUpperCase( string ){
      return string[1].toUpperCase();
    }
    
    Output: hyphenDelimitedToCamelCase
    

提交回复
热议问题