Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
How to decrease size of a string by using preceding numbers - JavaScript?
Let’s say our original string is the following with repeated letters −
var values = "DDAAVIDMMMILLERRRRR";
We want to remove the repeated letters and precede letters with numbers. For this, use replace() along with regular expression.
Example
Following is the code −
var values = "DDAAVIDMMMILLERRRRR";
var precedingNumbersInString = values.replace(/(.)\1+/g, obj => obj.length + obj[0]);
console.log("The original string value=" + values);
console.log("String value after preceding the numbers =");
console.log(precedingNumbersInString);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo295.js.
Output
This will produce the following output on console −
PS C:\Users\Amit\javascript-code> node demo295.js The original string value=DDAAVIDMMMILLERRRRR String value after preceding the numbers = 2D2AVID3MI2LE5R
Advertisements
