
- 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
Function to decode a string in JavaScript
We are given an encoded string, and we are required to process it through a function that returns its decoded string.
The encoding rule is −
n[encodedString], where the encodedString inside the square brackets is being repeated exactly n times.
And n is guaranteed to be a positive integer.
We can assume that the input string is always valid; No extra white spaces, square brackets are well−formed, etc.
For example − If the input is −
const str = "3[a]2[bc]";
Then the output should be −
const output: "aaabcbc";
Example
The code for this will be −
const str = "3[a]2[bc]"; const helper = (str = '') => { return str.replace(/(\d+\[\w+\])/gi, item => { let match = /(\d+)\[(\w+)\]/.exec(item); let repeat = parseInt(match[1]); let pattern = match[2]; let result = ""; while(repeat−− > 0) { result += pattern; } return result; }); }; const decodeString = function(str) { while(/\d+\[\w+\]/gi.test(str)) { str = helper(str); } return str; }; console.log(decodeString(str));
Output
And the output in the console will be −
aaabcbc
- Related Articles
- How to decode a URL using JavaScript function?
- How to decode an encoded string in JavaScript?
- Decode String in C++
- How to decode the string in android?
- Function to reverse a string JavaScript
- How to encode and decode a URL in JavaScript?
- Java Program to decode string to integer
- How to convert a string in to a function in JavaScript?
- How to transform a String into a function in JavaScript?
- How to create a function from a string in JavaScript?
- String function to replace nth occurrence of a character in a string JavaScript
- PHP – How to decode a MIME header field using iconv_mime_decode() function?
- How to return a string from a JavaScript function?
- To decode string array values that is already encoded in Numpy
- How to turn a String into a JavaScript function call?

Advertisements