
- 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
Replacing every nth instance of characters in a string - JavaScript
We are required to write a JavaScript function that takes in a string as the first argument, a number, say n, as the second argument and a character, say c, as the third argument. The function should replace the nth appearance of any character with the character provided as the third argument and return the new string.
Example
Following is the code −
const str = 'This is a sample string'; const num = 2; const char = '*'; const replaceNthAppearance = (str, num, char) => { const creds = str.split('').reduce((acc, val, ind, arr) => { let { res, map } = acc; if(!map.has(val)){ map.set(val, 1); if(num === 0){ res += char; }else{ res += val; } }else{ const freq = map.get(val); if(num - freq === 1){ res += char; }else{ res += val; }; map.set(val, freq+1); }; return { res, map }; }, { res: '', map: new Map() }); return creds.res; } console.log(replaceNthAppearance(str, num, char));
Output
Following is the output in the console −
This ***a s*mple string
- Related Articles
- Replacing all special characters with their ASCII value in a string - JavaScript
- Replacing upperCase and LowerCase in a string - JavaScript
- Finding sum of every nth element of array in JavaScript
- Replacing dots with dashes in a string using JavaScript
- Regrouping characters of a string in JavaScript
- Check if characters of a string can be made non-decreasing by replacing ‘_’s
- Insert a character after every n characters in JavaScript
- String function to replace nth occurrence of a character in a string JavaScript
- Replacing vowels with their 1-based index in a string in JavaScript
- Program to count number of distinct characters of every substring of a string in Python
- Replacing Substrings in a Java String
- JavaScript: take every nth Element of Array and display a fixed number of values?
- Reduce an array to the sum of every nth element - JavaScript
- Finding count of special characters in a string in JavaScript
- Number of non-unique characters in a string in JavaScript

Advertisements