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.

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

Similar Reads

1. Using Iterations Approach

In this specified approach, we are iterating through each character of the given input string by the user. The function checks characters one by on if there is space or not. If the space is encountered, then it is replaced with the “%20” sequence, else it appends the characters into the output string....

2. Using the Recursive Function Approach

In this approach, we are using the recursive function approach in which the function checks the character of the input string one by one. If the space is been encountered, then the function appends “%20” with the output string. The function continues this task, moving through the string recursively till all the characters from the string are been processed....

3. Using split() map() and join() Function Approach

In this approach, we are using the ‘split()‘, ‘map()‘, and ‘join()‘ functions to replace the spaces with “%20” encoding. Firstly, the input string is been split into an array of characters, By using the map() function we iterate through each character and replace the spaces with the “%20” and other characters are unchanged. After replacing, the array is rejoined into the single output string by using the ‘join()’ function....

4. Using replace() function Approach

In this simple approach, we are using the replace() function in JavaScript. This function uses the regular expression (‘/ /g’). The ‘g‘ flag used here assures that all occurrences of spaces are properly replaced with the “%20” encoding sequences throughout the entire string....

5. Using reduce() function Approach

In this approach, we will be using the ‘reduce()‘ function to URLify the inputted string. Firstly, we will start by splitting the input string into the array of characters, Then we will use the reduce() function to iterate through the array, by accumulating the URLified string character by character. For the individual character in the array, we will check its space, in which case we append “%20” or we will append the character as it is....

6. Using replaceAll() Function Approach

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....