Compressing String in JavaScript Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Compressing a string involves replacing repeated characters with their count followed by the character itself. For example, string aaabbccc can be compressed to 3a2b3c. Examples:Input: string = aaabbbcccOutput: compressed string = 3a3b3c Input: string = heeeeellllllooooooOutput: compressed string = 1h5e6l60Table of Content Using JavaScript loopsUsing Regular ExpressionsUsing JavaScript loopsWe can solve this problem by iterating through the string and count consecutive characters using the JavaScript loop and append the corresponding count and the character to a new string. Example: The below code will explain the use of JavaScript loops to compress a string. JavaScript function compressStringLoop(str) { let compressed = ''; let count = 1; for (let i = 0; i < str.length; i++) { if (str[i] === str[i + 1]) { count++; } else { compressed += count + str[i]; count = 1; } } console.log(compressed); } compressStringLoop("aaabbccc"); compressStringLoop("heeellllooooo"); Output3a2b3c 1h3e4l5o Using Regular ExpressionsWe can solve this problem by using a regular expression to match consecutive characters and replace them with their count followed by the character using the replace() method. Example: The below code uses the regular expressions to compress the given string in JavaScript. JavaScript function compressStringRegex(str) { return str.replace( /(.)\1*/g, (match, char) => match.length + char); } let compressedStrRegex1 = compressStringRegex("aaabbbcc"); let compressedStrRegex2 = compressStringRegex("heeellllooooo"); console.log(compressedStrRegex1); console.log(compressedStrRegex2); Output3a3b2c 1h3e4l5o Comment More infoAdvertise with us Next Article Spring Boot Tutorial A am8254s3a Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i 6 min read Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc 15+ min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read Like