
- 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
Validating a string with numbers present in it in JavaScript
Problem
We are required to write a JavaScript function that takes in a string str. Our function should validate the alphabets in the string based on the numbers before them.
We need to split the string by the numbers, and then compare the numbers with the number of characters in the following substring. If they all match, the string is valid and we should return true, false otherwise.
For example −
5hello4from2me
should return true
Because when split by numbers, the string becomes ‘hello’, ‘from’, ‘me’ and all these strings are of same length as the number before them
Example
Following is the code −
const str = '5hello4from2me'; const validateString = (str = '') => { const lenArray = []; let temp = ''; for(let i = 0; i < str.length; i++){ const el = str[i]; if(+el){ lenArray.push([+el, '']); }else{ const { length: len } = lenArray; lenArray[len - 1][1] += el; }; }; return lenArray.every(sub => sub[0] === sub[1].length); }; console.log(validateString(str));
Output
Following is the console output −
true
- Related Articles
- Validating brackets in a string in JavaScript
- Picking all the numbers present in a string in JavaScript
- Difference between numbers and string numbers present in an array in JavaScript
- Validating string with reference to array of words using JavaScript
- Summing numbers present in a string separated by spaces using JavaScript
- Validating a power JavaScript
- Reversing words present in a string in JavaScript
- Validating a password using JavaScript
- Validating a file size in JavaScript while uploading
- Validating push pop sequence in JavaScript
- Validating a square in a 2-D plane in JavaScript
- Product of numbers present in a nested array in JavaScript
- Validating a boggle word using JavaScript
- Validating alternating vowels and consonants in JavaScript
- Validating email and password - JavaScript

Advertisements