Convert dash-separated string to camelCase?

后端 未结 13 1165
爱一瞬间的悲伤
爱一瞬间的悲伤 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:54

    here is the jsfiddle you can play with to test this http://jsfiddle.net/5n84w/2/

    ```

    /**
     * Function to convert any string to camelCase
     * var regex = 'chetan-Ankola###.com---m13ok#-#alo(*finding!R%S#%-GFF'; 
     * Where [-_ .] is the seperator, you can add eg: '@' too
     * + is to handle repetition of seperator           
     * ? is to take care of preceeding token 
     * match nov(ember)? matches nov and november
     */
    var camelCaser = function (str) {
        var camelCased = str.replace(/[-_ .]+(.)?/g, function (match, p) {
            if (p) {
                return p.toUpperCase();
            }
            return '';
        }).replace(/[^\w]/gi, '');
        return camelCased;
    };
    

    ```

    0 讨论(0)
提交回复
热议问题