How to use replaceAll() Function Approach In Javascript
In this approach, we use the replaceAll() function in JavaScript, which allows for replacing all occurrences of a specified substring with another substring. This method simplifies the task of URLifying the input string by directly substituting all spaces with the “%20” encoding.
Example: In this example, we are using the replaceAll() function in JavaScript.
// Using replaceAll() function Approach
let inputStr = "Geeks for Geeks";
let outputStr = inputStr.replaceAll(' ', '%20');
console.log(outputStr);
Output
Geeks%20for%20Geeks
Time complexity: O(N), where N is the length of the input string. The replaceAll() function internally iterates over the entire string to replace all occurrences of the specified substring.
Auxiliary space: O(1). The replaceAll() function performs the replacement in-place and does not create additional data structures that depend on the size of the input string.
JavaScript URLify a given string (Replace spaces is %20)
In this article, we are going to discuss how can we URLify a given string using JavaScript. In which we will be transforming the spaces within the input strings into the “%20” sequence. The process of URLLification consists of replacing these spaces with the ‘%20’ encoding. This task is essential for creating valid URLs, and query parameters.
Example:
Input: "Geeks for Geeks"
Output: Geeks%20for%20Geeks
These are the following approaches by using these you can get the URLify string:
Table of Content
- Using Iterations Approach
- Using the Recursive Function Approach
- Using split() map() and join() Function Approach
- Using replace() function Approach
- Using reduce() function Approach
- Using replaceAll() Function Approach