
- 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
Create a polyfill to replace nth occurrence of a string JavaScript
Let’s say, we have created a polyfill function removeStr() that takes in three arguments, namely −
subStr → the occurrence of which String is to be removed
num → it’s a Number (num)th occurrence of subStr is to be removed from string
The function should return the new if the removal of the subStr from the string was successfully, otherwise it should return -1 in all cases.
For example −
const str = 'drsfgdrrtdr'; console.log(str.removeStr('dr', 3));
Expected output −
'drsfgdrrt'
Let’s write the code for this −
Example
const str = 'drsfgdrrtdr'; const subStr = 'dr'; const num = 2; removeStr = function(subStr, num){ if(!this.includes(subStr)){ return -1; } let start = 0, end = subStr.length; let occurences = 0; for(; ;end < this.length){ if(this.substring(start, end) === subStr){ occurences++; }; if(occurences === num){ return this.substring(0, start) + this.substring(end, this.length); }; end++; start++; } return -1; } String.prototype.removeStr = removeStr; console.log(str.removeStr(subStr, num));
Output
Output for this code in the console will be −
drsfgrtdr
Understanding the code −
The removeStr() function firstly checks if there’s not even a single occurrence of subStr then we should exit out and return -1.
Then it uses the sliding window algorithm to record the number of occurrences of subStr in string(the size of window being equal to length of subStr).
Initially we start from leftmost window then we keep sliding our window till the end of window reaches the end of original string.
If in our way, the count of occurrences equals the required occurrence, we trim that occurrence from the string and return the new string thus obtained.
If we iterate through the whole string, it means that there are not enough occurrences of subStr in string and in which case we should return -1 and exit the function
Lastly, we add the removeStr property to String.prototype so that we can call it as a string function.
- Related Articles
- String function to replace nth occurrence of a character in a string JavaScript
- C program to replace all occurrence of a character in a string
- How to find the nth occurrence of substring in a string in Python?
- Python - Replace duplicate Occurrence in String
- How to replace the last occurrence of an expression in a string in Python?
- Replace words of a string - JavaScript
- How to count the occurrence of a specific string in a string in JavaScript
- Replace first occurrence of a character in Java
- How to replace all occurrences of a string in JavaScript?
- Does JavaScript have a method to replace part of a string without creating a new string?
- How to use polyfill in JavaScript?
- Replacing every nth instance of characters in a string - JavaScript
- JavaScript - Writing a string function to replace the kth appearance of a character
- Replace alphabets with nth forward alphabet in JavaScript
- Replace all occurrence of specific words in a sentence based on an array of words in JavaScript
