Replace Multiple Words with K in JavaScript
Last Updated :
19 Feb, 2024
Sometimes, while working with Javascript strings, we may face a problem in which we have to perform a replacement of multiple words with a single word. This can have applications in many domains like day-day programming and school programming.
These are the following approaches:
Using join() and split() methods
The combination of the join() and split() functions can be used to perform this task. In this, we split the string into words, check and replace the list words using join and list.
Example: This example shows the use of the join() and split() methods to replace words.
Javascript
let test_str =
'Geeksforgeeks is best for geeks and CS' ;
console.log( "The original string is : " + test_str);
let word_list = [ "best" , 'CS' , 'for' ];
let repl_wrd = 'gfg' ;
let res = test_str.split( ' ' ).map(word =>
word_list.includes(word) ? repl_wrd : word).join( ' ' );
console.log( "String after multiple replace : " + res);
|
Output
The original string is : Geeksforgeeks is best for geeks and CS
String after multiple replace : Geeksforgeeks is gfg gfg geeks and gfg
Time Complexity: O(n)
Space Complexity: O(n)
Using Regex and join() method
Now we will use combination of regex and join() method to get the desired result. In JavaScript, the join() method and regular expressions (regex) are both valuable tools for working with text, but they serve distinct purposes. The join() method combines elements of an array or iterable into a single string, using a specified separator between each element. Regex are powerful patterns used to search, extract, and manipulate text based on defined patterns.
Example: This example shows the use of regular expression to replace the word.
Javascript
let test_str =
'Geeksforgeeks is best for geeks and CS' ;
console.log( "The original string is : " + test_str);
let word_list = [ "best" , 'CS' , 'for' ];
let repl_wrd = 'gfg' ;
let pattern = new RegExp(word_list.join( '|' ),
'g' );
let res = test_str.replace(pattern, repl_wrd);
console.log( "String after multiple replace : "
+ res);
|
Output
The original string is : Geeksforgeeks is best for geeks and CS
String after multiple replace : Geeksgfggeeks is gfg gfg geeks and gfg
Time Complexity: O(n)
Space Complexity: O(n)
Using for Loop and replace() method
Now we will use for loop and replace() method to get the required output. The replace() method will be used to search a string for a value. It usually returns a new string with the value(s) replaced. So we will run the for loop till the size of word_list.
Example: This example shows the use of loop and replace() method to replace the word.
Javascript
let test_str = `Geeksforgeeks is best
for computer science and IT`;
console.log( "The original string is : " + test_str);
let word_list = [ "best" , 'IT' , 'for' ];
let repl_wrd = 'gfg' ;
for (let i = 0; i < word_list.length; i++) {
test_str = test_str.replace( new RegExp(word_list[i],
'g' ), repl_wrd);
}
console.log( "String after multiple replace : " + test_str);
|
Output
The original string is : Geeksforgeeks is best
for computer science and IT
String after multiple replace : Geeksgfggeeks is gfg
gfg computer science and gfg
Time Complexity: O(n*m) where n is the length of the input string and m is the number of words in the word_list.
Space Complexity: The space complexity is O(n), where n is the length of the input string test_str.
Using map() method
Another method to solve the problem is by splitting the string into an array of words, then using the map() method to iterate over each word and replace it if it exists in the word_list. Finally, we join the modified array back into a string.
Example: This example shows the use of map() method to replace the word.
Javascript
let test_str = `Geeksforgeeks is best
for computer science and IT`;
console.log( "The original string is : "
+ test_str);
let word_list = [ "best" , 'CS' , 'for' ];
let repl_wrd = 'gfg' ;
let res = test_str.split( ' ' ).map(word => {
if (word_list.includes(word)) {
return repl_wrd;
}
return word;
}).join( ' ' );
console.log( "String after multiple replace : "
+ res);
|
Output
The original string is : Geeksforgeeks is best
for computer science and IT
String after multiple replace : Geeksforgeeks is gfg
for computer science and IT
Time Complexity: O(n)
Space Complexity: O(n)
Share your thoughts in the comments
Please Login to comment...