![]() |
In this article, we are going to learn about replacing specific words with another word in a string using regular expressions. Replacing specific words with another word in a string using regular expressions in JavaScript means searching for all occurrences of a particular word or pattern within the text and substituting them with a different word or phrase, considering possible variations and multiple instances. There are several methods that can be used to replace specific words with another word in a string using regular expressions in JavaScript, which are listed below: Table of Content We will explore all the above methods along with their basic implementation with the help of examples. Approach 1: Using String.replace()In this approach, we are Using String.replace() with a regular expression allows you to find and replace specific words or patterns within a string efficiently, enabling global replacements for all occurrences of the target. Syntax: function replaceWord(val1, val2, val3) { Example: In this example, The replaceWord function uses a regular expression to replace all occurrences of ‘gfg’ with ‘GeeksforGeeks’ in the input text, demonstrating word replacement.
Output GeeksforGeeks is a computer science portal. Approach 2: String.split() and Array.join() methodsIn this approach, Using String.split() to split a string into words, mapping and replacing specific words, and finally using Array.join() to rejoin the modified words into a new string efficiently. Syntax: let words = str1.split(/\s+|\W+/); Example: In this example, we split the string into words while removing punctuation, compare each word case-insensitively, and replace occurrences of ‘gfg’ with ‘GeeksforGeeks’, then rejoin the modified words.
Output GeeksforGeeks a Computer science Portal Approach 3: Using Word Boundary \b in Regular ExpressionThis method constructs a regular expression with word boundary `\b`, ensuring exact word matches. It replaces all occurrences of the target word with the new word, preserving word boundaries. This prevents unintended replacements within larger words. Example:
Output This is a example string Approach 4: Using a Map Object for ReplacementIn this approach, we can use a map object to store the words we want to replace and their corresponding replacements. We iterate through the map object and use regular expressions to replace all occurrences of each word with its replacement in the string.
Output Replace all occurrences of 'GeeksforGeeks' with 'GeeksforGeeks'. GeeksforGeeks is awesome! Approach 5: Using a Callback Function in String.replace()In this approach, we use a callback function within String.replace() to customize the replacement logic based on more complex criteria. This method provides flexibility to handle context-sensitive replacements or incorporate additional logic during the replacement process. Example: In this example, we use a callback function to replace all occurrences of the target word ‘gfg’ with ‘GeeksforGeeks’, but only if the word is not part of a larger word. The callback checks if the matched word is exactly ‘gfg’ and performs the replacement accordingly.
Output GeeksforGeeks is a computer science portal. Visit GeeksforGeeks! |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |