Capitalize a word and replace spaces with underscore - JavaScript?


We will try to develop a method to change text with spaces and lowercase letters into text with underscores, where the initial letter of each word is capitalized. Let's examine the equation in its entirety.

tutorials point = Tutorials_Point //expected output

Let’s dive into article for getting better understanding on capitalize a word and replace spaces with underscore JavaScript.

Using replace() method

The replace() method looks up a value or regular expression in a string. A new string with the replaced value() is the result of the replace() method. The original string is unchanged by the replace() technique.

Syntax

Following is the syntax for replace()

string.replace(searchValue, newValue)

For better understanding on capitalizing a word and replace spaces with underscore JavaScript, let’s look into the following examples.

Example

In the following example, we are running the script to capitalize the word and replace spaces with underscores by passing a regular expression.

<!DOCTYPE html>
<html>
<body>
   <script>
      document.write('welcome to the tutorials point'.toUpperCase().replace(/ /g, '_')
      .replace(/(?: |\b)(\w)/g, function(key, p1) {
         return key.toUpperCase();
      }));
   </script>
</body>
</html>

When the script gets executed, it will generate an output consisting of text where the spaces are replaced with underscores and words are capitalized and displayed on the webpage.

Example

Run the below code to see how replace() works in capitalizing the word and replacing spaces with underscores.

<!DOCTYPE html>
<html>
<body>
   <script>
      var sentence = "My favorite subject is javascript. I live in us.";
      var result = sentence.trim().replace(/ /g, '_').toLocaleUpperCase();
      document.write(result);
   </script>
</body>
</html>

When you run the above script, an output window will appear, displaying the text with spaces replaced with underscores and words capitalized.

Using split() method

The split() method divides a String into an ordered list of substrings by searching for the pattern; these substrings are then placed into an array, which is then returned.

Syntax

Following is the syntax for split()

string.split(separator, limit)

Example

Consider the following example, here we are running the script to replace spaces with underscores and capitalize the word.

<!DOCTYPE html>
<html>
<body>
   <script>
      function change(str) {
         var i, frags = str.split(' ');
         for (i = 0; i < frags.length; i++) {
            frags[i] = frags[i].charAt(0).toUpperCase() + frags[i].slice(1);
         }
         return frags.join('_');
      }
      document.write(change('the best e-way learning'));
   </script>
</body>
</html>

On running the above script, the output window will pop up, displaying the text that got replaced with an underscore and got capitalized.

Updated on: 21-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements