Convert dash-separated string to camelCase?

后端 未结 13 1261
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 11:01

For example suppose I always have a string that is delimited by \"-\". Is there a way to transform

it-is-a-great-day-today

to

itIsAGreatDayToday

13条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 11:33

    See http://jsfiddle.net/54ZcM/

    function camelCase(string) {
        return string.toLowerCase().replace(/(\-[a-zA-Z])/g, function($1) {
            return $1.toUpperCase().replace('-','');
        })
    }
    
    alert(camelCase('fOo-BarBA-fo'));
    

提交回复
热议问题