How to detect and replace all the array elements in a string using RegExp in JavaScript? 


An array is an ordered list of values in JavaScript. The term "element designated by an index" refers to each value: The following traits describe a JavaScript array: An array can initially store values of many sorts. You could, for instance, store elements of the number, string, Boolean, and null types in an array.

Let’s jump into the article to detect and replace all the array elements in a string using regexp in JavaScript.

Using regexp in JavaScript

A regular expression is a character pattern. Pattern-matching "search-and-replace" operations are performed on text using the pattern. A RegExp Object is a pattern with Properties and Methods in JavaScript.

Syntax

Following is the syntax for regexp

/pattern/modifier(s);

For getting better understanding on detecting and replacing all the array elements in a string using the regexp, let’s look into the following examples.

Example

In the following example we are running a script to replace all the array elements along with a regexp.

<!DOCTYPE html>
<html>
   <body>
      <script>
         let array = ['duke', 'rx100']
         let t = "rx100 is more better than the duke."
         let regexp = new RegExp(array.join('|'), 'gim')
         t = t.replace(regexp, 'N/A')
         document.write(t)
      </script>
   </body>
</html>

When the script is run, it will generate output consisting of a sentence with "N/A" replaced wherever the array elements are found in the sentence due to an event that is triggered when the script is run.

Example

Consider the following example, here we are using the regexp to replace the array elements in a string.

<!DOCTYPE html>
<html>
   <body>
      <p id="tutorial"></p>
      <script>
         var string = "Welcome to the tp. the best e-way learning";
         var replaceArr = ["welome", "tp", "best", "e-way"];
         var replaceArrayValue = ["Welcome", "Tutorialspoint", "Best", "E-way"];
         var result = string;
         for (var i = replaceArr.length - 1; i >= 0; i--) {
            result = result.replace(RegExp("\b" + replaceArr[i].replace(/[-\/\^$*+?.()|[\]{}]/g, '\$&') + "\b", "g"), replaceArrayValue[i]);
         }
         document.getElementById("tutorial").innerHTML = result;
      </script>
   </body>
</html>

On running the above script, the output window will pop up, displaying the sentence that got replaced by the elements mentioned in the script as a result of the event that got triggered on running the script.

Example

Let’s execute the below code to observe how array elements are detected and replaced in a string using regexp.

<!DOCTYPE html>
<html>
   <body>
      <p id="tutorial"></p>
      <script>
         var values = ['John', 'Smith', 'UK', 'JavaScript']
         var sentence = "My Name is John Smith. I live in UK. My Favourite Subject is JavaScript."
         var regularExpression = new RegExp(values.join('|'), 'gim')
         sentence = sentence.replace(regularExpression, 'Not Available');
         document.getElementById("tutorial").innerHTML = sentence;
      </script>
   </body>
</html>

When the script gets executed, the event gets triggered, displaying a sentence replaced with "not available" for the elements that got matched with the regexp.

Updated on: 21-Apr-2023

839 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements