HomeTutorialsArticlesAbout MeContact

JS Challenge 6: Convert string to camel case

By Alberto Montalesi
Published in Challenge
October 25, 2020
1 min read
Check out my Github for my free-to-read JavaScript Ebook that covers all the new features from ES6 to 2021. If you want find a great place for interactive tutorials, i recommend Educative where you can find my JavaScript course
This website contains affiliate links.

In this article we will solve together the Convert string to camel case challenge from CodeWars, you can find it at this link. The difficulty of this challenge is easy.

Let’s read the task together:

Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case).

Examples toCamelCase(“the-stealth-warrior”) // returns >“theStealthWarrior”

toCamelCase(“The_Stealth_Warrior”) // returns “TheStealthWarrior”

The easiest way to solve this challenge will be with RegEx and we don’t even need a complicate one in this case:

Write the RegExp

/[-_]\w/ig this is all we need.

  • [-_] will match all the underscore and dashes
  • \w will match any character right after a dash or underscore
  • /ig will perform a global case insesitive search.

The trick that will help us complete the challenge more easily is the \w which allows us to capture the letter right after a dash or underscore, meaning we can easily make it uppercase in one go.

When you want to play around with RegEx, this is a great playground that also explains how they work regexr.com.

Now that we have the most important piece of our function, let’s try and build a function around it.

function toCamelCase(str){
      const regExp = /[-_]\w/ig;
      return str.replace(regExp,(match) => {
          console.log(match);
       });
}
toCamelCase("the-stealth-warrior")
// -s
// -w

String.replace() can take not only a substring but only a RegExp as the first parameter and will pass the result in the second one.

As you can see our RexExp matched each dash and the first character after.

Completing the function

Now what we are left to do with the function is to return only the uppercase letter without the dash or underscore.

function toCamelCase(str){
      const regExp = /[-_]\w/ig;
      return str.replace(regExp,(match) => {
          return match[1].toUppercase()
       });
}
toCamelCase("the-stealth-warrior")

That’s it, the last line we added will return S instead of -s and W instead of -w.

There are many other ways of solving this problem, let me know yours in the comment.

If you liked this type of content, please let me know in the comments and I’ll create more of these.



Tags

challenge
Previous Article
JS Challenge 5: Pete the baker

Alberto Montalesi

Software Developer

buy me coffee
complete guide to modern javascript book cover

Complete Guide to Modern JavaScript

Get the Course

Category

Article
Challenge
Tutorial

Related Posts

JS Challenge 7: Multiples of 3 or 5
October 28, 2020
1 min
© 2022, All Rights Reserved.

Quick Links

TutorialsArticlesChallenges

Social Media