Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

正则 驼峰转换 #54

Open
Sunny-117 opened this issue Nov 3, 2022 · 3 comments
Open

正则 驼峰转换 #54

Sunny-117 opened this issue Nov 3, 2022 · 3 comments

Comments

@Sunny-117
Copy link
Owner

//横线式
let str = 'hello-world'
function getCamelCase(str) {
    let reg = /-(\w)/g;
    return str.replace(reg, (_, match) => match.toUpperCase())
}
console.log(getCamelCase(str))// helloWorld
@lzxjack
Copy link
Contributor

lzxjack commented Jan 12, 2023

const changeName = str => str.replace(/-([a-z])/g, (all, char) => char.toUpperCase());

@kangkang123269
Copy link

kangkang123269 commented Feb 25, 2023

  1. 驼峰 转换 -拼接字符
str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); // helloWorld => hello-world
  1. -拼接字符 转换 驼峰
str.replace(/-([a-z])/g, function(match, group1) {
  return group1.toUpperCase();
});
// hello-world => helloWorld

@veneno-o
Copy link
Contributor

// 拼接字符 转换 驼峰
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants