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: 2020-06-23T05:37:40+05:30

200 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements