How to write JavaScript Regular Expression for multiple matches?


To find multiple matches, write a JavaScript regular expression. You can try to run the following code to implement regular expression for multiple matches −

Example

<html>
   <head>
      <script>
         var url = 'https://www.example.com/new.html?ui=7&demo=one&demo=two&demo=three four',
         a = document.createElement('a');
         a.href = url;

         var demoRegex = /(?:^|[&;])demo=([^&;]+)/g,
         matches,
         demo = [];

         while (matches = demoRegex.exec(a.search)) {
            demo.push(decodeURIComponent(matches[1]));
         }

         document.write(demo);
      </script>
   </head>
   <body>
   </body>
</html>

Updated on: 23-Jun-2020

94 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements