FED31. 将字符串转换为驼峰格式
描述
css 中经常有类似 background-image 这种通过 - 连接的字符,通过 javascript 设置样式的时候需要将这种样式转换成 backgroundImage 驼峰格式,请完成此转换功能示例1
输入:
'font-size'
输出:
fontSize
HTML/CSS/JavaScript 解法, 执行用时: 861ms, 内存消耗: 77900KB, 提交时间: 2021-01-17
{"css":"","js":"function cssStyle2DomStyle(sName) {\n const reg = /-(.)/g;\n return sName.replace(reg, (fullMatch, g1, index) => {\n if (index === 0) return g1;\n return g1.toUpperCase();\n });\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 865ms, 内存消耗: 77848KB, 提交时间: 2020-11-05
{"css":"","js":"function cssStyle2DomStyle(sName) {\n //split切割函数\n var arr = sName.split('');\n //判断第一个是不是 - ,是的话就删除、\n if(arr.indexOf('-') ==0 ){\n arr.splice(0,1);\n }\n \n //处理剩余的 -\n for(var i = 0;i < arr.length; i++){\n if(arr[i] == '-'){\n arr.splice(i ,1);\n arr[i] = arr[i].toUpperCase();\n }\n }\n //根据某个字符将数组转成字符串\n return arr.join('');\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 865ms, 内存消耗: 77860KB, 提交时间: 2020-12-06
{"css":"","js":"function cssStyle2DomStyle(sName) {\n let res = sName.replace(/([a-zA-Z0-9])-([a-zA-Z])/g, \n function($1,$2,$3){\n ;return $2+$3.toUpperCase()})\n return res.replace(/-/g, '')\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 865ms, 内存消耗: 77868KB, 提交时间: 2020-11-04
{"css":"","js":"function cssStyle2DomStyle(sName) {\n return sName.replace(/-[a-zA-Z]{1}/g, function($1,$2) {\n return $2 === 0? $1[1]: $1.toUpperCase()[1]\n })\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 866ms, 内存消耗: 77792KB, 提交时间: 2021-01-17
{"css":"","js":"function cssStyle2DomStyle(sName) {\n let arr = sName.split('-')\n let res = arr[0] ? arr[0] : arr[0] + arr[1]\n for(let i = arr[0] ? 1 : 2; i <= arr.length; i++) {\n arr[i] && (res += (arr[i][0].toUpperCase() + arr[i].slice(1)))\n }\n console.log(res)\n return res\n}\ncssStyle2DomStyle('-font-size')","html":"","libs":[]}