Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

正则:驼峰 => 短横线

lzxjack opened this issue · comments

const changeName = str =>
  str
    .split(/(?=[A-Z])/)
    .join('-')
    .toLowerCase();
// 拼接字符 转换 驼峰
let str = 'hello-world-mihayou';
str = str.replace(/-([a-z])/g,function(text, $1){
    return $1.toUpperCase();
})
console.log(str);

// 驼峰 转换 -拼接字符
str = str.replace(/([a-z])([A-Z])/g,function(text, $1, $2){
    return $1 + "-" +$2.toLowerCase();
})
console.log(str)