开发者

JavaScript RegExp to CamelCase a hyphened CSS property

I am trying to change CSS properties like this one.

-moz-border-radius

To the Ja开发者_如何学CvaScript CSS property like so.

MozBorderRadius

I am using this RegExp.

var exp = new RegExp('-([a-z])', 'gi');
console.log('-moz-border-radius'.replace(exp, '$1'));

All I need to do is convert $1 to upper case so it can cammelcaseify (yes I made that word up...) my CSS property into a JavaScript based one. Is this possible?

Thanks.


You would be better off using a function as the second parameter in replace(), and you could also use a regex literal instead of the RegExp constructor:

var replaced = '-moz-border-radius'.replace(/-([a-z])/gi, function(s, group1) {
    return group1.toUpperCase();
});


You need to pass a callback function instead of a string.

For example:

var exp = /-([a-z])/gi;
console.log('-moz-border-radius'.replace(exp, 
    function(match, char, index, str) {
        return char.toUpperCase();
    }
));


Another, slightly more flexible answer:

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:

'-moz-border-radius'.toCamel(); // "MozBorderRadius"
'moz-border-radius'.toCamel(); // "mozBorderRadius"
'moz_border_radius'.toCamel(); // "mozBorderRadius"
'_moz_border_radius'.toCamel(); // "MozBorderRadius"


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


is also possible use indexOf with recursion for that task.

input some-foo_sd_dsd-weqe
output someFooSdDsdWeqe

but is works slower

MozBorderRadius
test1: 3.535ms

code:

function camelCased (str) {

        function check(symb){

            let idxOf = str.indexOf(symb);
            if (idxOf === -1) {
                return str;
            }

            let letter = str[idxOf+1].toUpperCase();
            str = str.replace(str.substring(idxOf+1,idxOf+2), '');
            str = str.split(symb).join(idxOf !== -1 ? letter : '');

            return camelCased(str);
        }       

        return check('_') && check('-');

    }

console.log(camelCased ('-moz-border-radius'));


if you need to convert an entire object of hypen-delimited keys to camelCase:

const css2js = (obj) => Object.fromEntries(Object.entries(obj).map(x => [x[0].replace(/(-)([a-z])/g, c => c[1].toUpperCase()), x[1]]));

example

const a = {
    "background-color": "#00aa",
    marginTop: "10px"
};

console.log(css2js(a)); // {backgroundColor: "#00aa", marginTop: "10px"}

https://codepen.io/oriadam/pen/eYBNeyP

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜