- First, capture a group of
_and the next letter afterwards.
- Remove both
_from the captured group.
- Replace the remaining letter with its uppercase version.
Pretty straightforward, right? Let’s see how it looks in ES6:
const snakeToCamel = (str) => str.replace( /([-_][a-z])/g, (group) => group.toUpperCase() .replace('-', '') .replace('_', '') ); snakeToCamel('my-snake-string'); // mySnakeString
Loving these short articles? Make sure to subscribe!
Be the first to know when I publish a new article on the blog.
No spam. You can unsubscribe at any time.