function toCamel(str) {
return str.replace(/([^_])(?:_+([^_]))/g, function ($0, $1, $2) {
console.log($0, $1, $2)
return $1 + '_' + $2.toUpperCase();
});
}
console.log(toCamel('test_to_camel'))
现在只能拿到test_To_Camel
想把第一个字母转大写的话正则该怎么改呢
function toCamel(str) {
return str.replace(/([^_])(?:_+([^_]))/g, function ($0, $1, $2) {
console.log($0, $1, $2)
return $1 + '_' + $2.toUpperCase();
});
}
console.log(toCamel('test_to_camel'))
现在只能拿到test_To_Camel
想把第一个字母转大写的话正则该怎么改呢
补充另一种正则
// => Test_To_Camel Abc_Def_G
'test_to_camel abc_def_g'.replace(/(\b|_)\w/g, item => item.toUpperCase());
13 回答13k 阅读
7 回答2.1k 阅读
3 回答1.3k 阅读✓ 已解决
6 回答1.2k 阅读✓ 已解决
2 回答1.4k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
6 回答1.1k 阅读