JavaScript Insert space after every two letters in string?

In this article we are going to learn about how to insert space after every two letters in string. Before we jump into the article let's have a quick view on the strings in JavaScript.

A sequence of one or more characters?which could be numbers, or symbols?is referred to as a string. Strings are immutable primitive data types in JavaScript, which means they cannot be changed.

Let's dive into the article to learn more about inserting a space after every two letters in strings. For this, we'll explore multiple approaches using replace() with regular expressions and other methods.

The replace() Method in JavaScript

The replace() method returns a new string with one, some, or all matches of a pattern replaced by a replacement. A string or a RegExp can be used as the pattern, and a function can be called for each match as the replacement.

Syntax

string.replace(searchValue, newValue)

Method 1: Using Regular Expression for Numbers

This approach uses regex /\d{2}(?=.)/g to match every two digits followed by another character:

<!DOCTYPE html>
<html>
<body>
   <script>
      function addSpaceToNumbers(s) {
         return s.toString().replace(/\d{2}(?=.)/g, '$& ');
      }
      
      let number = 9848004322148;
      let result = addSpaceToNumbers(number);
      document.write("Original: " + number + "<br>");
      document.write("With spaces: " + result);
   </script>
</body>
</html>
Original: 9848004322148
With spaces: 98 48 00 43 22 148

Method 2: Using Regex for Any Characters

This method works with both letters and numbers using /.{2}(?!$)/g:

<!DOCTYPE html>
<html>
<body>
   <script>
      function addSpaceEveryTwo(str) {
         return str.replace(/.{2}(?!$)/g, "$& ");
      }
      
      let text1 = "JavaScriptTutorial";
      let text2 = "1234567890";
      
      document.write("Original: " + text1 + "<br>");
      document.write("Result: " + addSpaceEveryTwo(text1) + "<br><br>");
      
      document.write("Original: " + text2 + "<br>");
      document.write("Result: " + addSpaceEveryTwo(text2));
   </script>
</body>
</html>
Original: JavaScriptTutorial
Result: Ja va Sc ri pt Tu to ri al

Original: 1234567890
Result: 12 34 56 78 90

Method 3: Using slice() and join()

This approach manually splits the string into chunks and joins them with spaces:

<!DOCTYPE html>
<html>
<body>
   <script>
      function formatString(str, chunkSize) {
         let chunks = [];
         let start = 0;
         
         while(start < str.length) {
            chunks.push(str.slice(start, start + chunkSize));
            start += chunkSize;
         }
         
         return chunks.join(" ");
      }
      
      let text = "welcometothetutorialspoint";
      let result = formatString(text, 2);
      
      document.write("Original: " + text + "<br>");
      document.write("Result: " + result);
   </script>
</body>
</html>
Original: welcometothetutorialspoint
Result: we lc om et ot he tu to ri al sp oi nt

Comparison of Methods

Method Best For Regex Complexity
Method 1 Numbers only Medium
Method 2 Any characters Medium
Method 3 Any characters, flexible chunk size None (manual splitting)

Conclusion

Use replace() with regex for concise solutions, or slice() with join() for more readable code. The regex approach /.{2}(?!$)/g is most versatile for inserting spaces after every two characters.

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

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements