 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- 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' ]
Advertisements
 