- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to split string when the value changes in JavaScript?
For this, you can use match() along with regular expression. Following is the code −
Example
var originalString="JJJJOHHHHNNNSSSMMMIIITTTTHHH"; var regularExpression=/(.)\1*/g; console.log("The original string="+originalString); var splitting=originalString.match(regularExpression); console.log("After splitting the original string="); console.log(splitting);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo139.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo139.js The original string=JJJJOHHHHNNNSSSMMMIIITTTTHHH After splitting the original string=[ 'JJJJ', 'O', 'HHHH', 'NNN', 'SSS', 'MMM', 'III', 'TTTT', 'HHH' ]
- Related Articles
- How to Split large string in to n-size chunks in JavaScript?
- How to add border line below when value changes in an Excel column?
- Split string into groups - JavaScript
- Trim and split string to form array in JavaScript
- How to split last n digits of each value in the array with JavaScript?
- Split string into equal parts JavaScript
- In how many ways can we split a string in JavaScript?
- How to split a string in Python
- How to split a string in Java?
- How to split a string in Golang?
- How to extract the split string elements in R?
- How to convert a boolean value to string value in JavaScript?
- How to get the primitive value of string in Javascript?
- How to concatenate the string value length -1 in JavaScript.
- How to split string by a delimiter string in Python?

Advertisements