-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-to-camelCase.js
More file actions
26 lines (17 loc) · 896 Bytes
/
string-to-camelCase.js
File metadata and controls
26 lines (17 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// "the-stealth-warrior" gets converted to "theStealthWarrior"
// "The_Stealth_Warrior" gets converted to "TheStealthWarrior"
// "The_Stealth-Warrior" gets converted to "TheStealthWarrior"
// function camelCase(str){
// const result = str.replace(/[-_]/g," ")
// console.log(result)
// }
// camelCase("the-stealth-warrior")
function camelCase(str) {
// Step 1: Remove all hyphens and underscores
const result = str.replace(/[-_/](.)/g,(_,c)=>c.toUpperCase()) //, the matching string , c captures the character after the symbol
//the capturing group (.) captures the single character that comes after either a hyphen - or an underscore _.
return result.charAt(0) + result.slice(1);
// Step 2: Convert the first character to lowercase
// return result.charAt(0).toLowerCase() + result.slice(1);
}
console.log(camelCase("the/stealth/warrior-hi_hello"))