
- 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
Loop through array and edit string JavaScript
Let’s say, we have to write a function, say translate() that accepts a string as the first argument and any number of words after that.
The string will actually contain n $ signs like this −
This $0 is more $1 just a $2. Then there will be 3 strings which will replace the corresponding places.
For example −
If the function call is like this −
translate(‘This $0 is more $1 just a $2.’, ‘game’, ‘than’, ‘game’);
The output of the function should be −
This game is more than just a game.
This functionality is more or less like the template injecting in JavaScript.
Therefore, let’s write the code for this function −
We will use the String.prototype.replace() method here. We know that if we use a regex pattern to match all occurrences and use a function as the second parameter, it gets executed for each match. We will do exactly the same here.
The code for doing this will be −
Example
const str = 'This $0 is more $1 just a $2'; const translate = (str, ...texts) => { const regex = /\$(\d+)/gi; return str.replace(regex, (item, index) => { return texts[index]; }); }; console.log(translate(str, 'game', 'just', 'game'));
Output
The output in the console will be −
This game is more just just a game
- Related Articles
- Recursively loop through an array and return number of items with JavaScript?
- Loop through an array in Java
- Loop through a Set using Javascript
- Loop through a Dictionary in Javascript
- Split a string and loop through values in MySQL Procedure?
- How do we loop through array of arrays containing objects in JavaScript?
- Loop through a hash table using Javascript
- How to use for...in statement to loop through an Array in JavaScript?
- How to loop through an array in Java?
- How do you loop through a C# array?
- Loop through an index of an array to search for a certain letter in JavaScript
- How to use for each loop through an array in Java?
- Loop backward in array of objects JavaScript
- Loop through all MongoDB collections and execute query?
- Figuring out the highest value through a for in loop - JavaScript

Advertisements