Capitalize a word and replace spaces with underscore - JavaScript?

In JavaScript, you can capitalize words and replace spaces with underscores using various string methods. This transformation is commonly needed for creating identifiers, constants, or formatted text.

let input = "tutorials point";
console.log("Input:", input);
// Expected output: "Tutorials_Point"
Input: tutorials point

Let's explore different methods to achieve this transformation in JavaScript.

Using replace() Method

The replace() method searches for a specified pattern in a string and replaces it with a new value. It accepts regular expressions for complex pattern matching.

Syntax

string.replace(searchValue, newValue)

Example 1: Basic Approach

<!DOCTYPE html>
<html>
<body>
   <script>
      let text = 'welcome to the tutorials point';
      
      // Split, capitalize first letter of each word, join with underscore
      let result = text.split(' ')
         .map(word => word.charAt(0).toUpperCase() + word.slice(1))
         .join('_');
      
      document.write(result);
   </script>
</body>
</html>

When executed, this code capitalizes the first letter of each word and replaces spaces with underscores.

Example 2: Using Regular Expression

<!DOCTYPE html>
<html>
<body>
   <script>
      let sentence = "my favorite subject is javascript";
      
      // Replace spaces with underscores, then capitalize first letter of each word
      let result = sentence.replace(/ /g, '_')
         .replace(/\b\w/g, function(match) {
            return match.toUpperCase();
         });
      
      document.write(result);
   </script>
</body>
</html>

This approach uses regular expressions to first replace spaces with underscores, then capitalize the first letter of each word.

Using split() Method

The split() method divides a string into an array of substrings based on a specified separator, allowing you to process each word individually.

Syntax

string.split(separator, limit)

Example

<!DOCTYPE html>
<html>
<body>
   <script>
      function capitalizeAndUnderscore(str) {
         let words = str.split(' ');
         
         for (let i = 0; i < words.length; i++) {
            words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1).toLowerCase();
         }
         
         return words.join('_');
      }
      
      let result = capitalizeAndUnderscore('the best e-learning platform');
      document.write(result);
   </script>
</body>
</html>

This function splits the string into words, capitalizes the first letter of each word while making the rest lowercase, then joins them with underscores.

Comparison

Method Complexity Readability Performance
replace() with regex Medium Low Good
split() and map() Low High Good
split() with loop Low Medium Best

Conclusion

The split() method combined with map() provides the most readable solution for capitalizing words and replacing spaces with underscores. Choose the approach that best fits your coding style and performance requirements.

Updated on: 2026-03-15T23:19:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements