
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Splitting string into groups – JavaScript
Given a string S, consisting of alphabets, numbers and special characters. We need to write a program to split the strings in three different strings S1, S2 and S3, such that −
- The string S1 will contain all the alphabets present in S,
- The string S2 will contain all the numbers present in S, and
- S3 will contain all special characters present in S.
The strings S1, S2 and S3 should have characters in the same order as they appear in input.
Example
Following is the code −
const str = "Th!s String C0nt@1ns d1fferent ch@ract5rs"; const seperateCharacters = str => { const strArr = str.split(""); return strArr.reduce((acc, val) => { let { numbers, alpha, special } = acc; if(+val){ numbers += val; }else if(val.toUpperCase() !== val.toLowerCase()){ alpha += val; }else{ special += val; }; return { numbers, alpha, special }; }, { numbers: '', alpha: '', special: '' }); }; console.log(seperateCharacters(str));
Output
This will produce the following output in console −
{ numbers: '115', alpha: 'ThsStringCntnsdfferentchractrs', special: '! 0@ @' }
- Related Articles
- Splitting an array into groups in JavaScript
- Splitting a string into parts in JavaScript
- Split string into groups - JavaScript
- Splitting a string into maximum parts in JavaScript
- Splitting an array into chunks in JavaScript
- Splitting Number into k length array in JavaScript
- JavaScript Splitting string by given array element
- Rearranging cards into groups in JavaScript
- Similar string groups in JavaScript
- Splitting an object into an array of objects in JavaScript
- Splitting number into n parts close to each other in JavaScript
- Splitting array of numbers into two arrays with same average in JavaScript
- Separating data type from array into groups in JavaScript
- Splitting a hyphen delimited string with negative numbers or range of numbers - JavaScript?
- Golang program to splitting into number of substrings

Advertisements