
- 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
Regrouping characters of a string in JavaScript
Problem
We are required to write a JavaScript function that takes in a string str as the first and the only argument.
The string str can contain three types of characters −
English alphabets: (A-Z), (a-z)
Numerals: 0-9
Special Characters − All other remaining characters
Our function should iterate through this string and construct an array that consists of exactly three elements, the first contains all alphabets present in the string, second contains the numerals and third the special characters maintain the relative order of characters. We should finally return this array.
For example, if the input to the function is
Input
const str = 'thi!1s is S@me23';
Output
const output = [ 'thisisSme', '123', '! @' ];
Example
Following is the code −
const str = 'thi!1s is S@me23'; const regroupString = (str = '') => { const res = ['', '', '']; const alpha = 'abcdefghijklmnopqrstuvwxyz'; const numerals = '0123456789'; for(let i = 0; i < str.length; i++){ const el = str[i]; if(alpha.includes(el) || alpha.includes(el.toLowerCase())){ res[0] += el; continue; }; if(numerals.includes(el)){ res[1] += el; continue; }; res[2] += el; }; return res; }; console.log(regroupString(str));
Output
[ 'thisisSme', '123', '! @' ]
- Related Articles
- Finding count of special characters in a string in JavaScript
- Number of non-unique characters in a string in JavaScript
- Switching positions of selected characters in a string in JavaScript
- Replacing every nth instance of characters in a string - JavaScript
- Counting the number of redundant characters in a string - JavaScript
- How to find unique characters of a string in JavaScript?
- Generate random string/characters in JavaScript?
- Remove characters from a string contained in another string with JavaScript?
- Formatting a string to separate identical characters in JavaScript
- Sorting string characters by frequency in JavaScript
- Placing same string characters apart in JavaScript
- Vowel, other characters and consonant difference in a string JavaScript
- Removing all non-alphabetic characters from a string in JavaScript
- Largest Substring Between Two Equal Characters in a string in JavaScript
- Removing n characters from a string in alphabetical order in JavaScript

Advertisements