
- 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
Split string into groups - JavaScript
Given a string S which consists 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
Following is the output in the console −
{ numbers: '115', alpha: 'ThsStringCntnsdfferentchractrs', special: '! 0@ @' }
- Related Articles
- Splitting string into groups – JavaScript
- Split string into equal parts JavaScript
- Split tuple into groups of n in Python
- Rearranging cards into groups in JavaScript
- Similar string groups in JavaScript
- How to a split a continuous variable into multiple groups in R?
- Splitting an array into groups in JavaScript
- Split string into sentences using regex in PHP
- Python – Split Numeric String into K digit integers
- Split number into n length array - JavaScript
- Number Split into individual digits in JavaScript
- Python program to split string into k distinct partitions
- How to split comma and semicolon separated string into a two-dimensional array in JavaScript ?
- Split keys and values into separate objects - JavaScript
- How to split JavaScript Number into individual digits?

Advertisements