How to write JavaScript Regular Expression for multiple matches?

JavascriptWeb DevelopmentFront End Technology

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>
raja
Published on 23-Feb-2018 06:45:58
Advertisements