Get all indexes of a pattern in a string - Javascript String

Javascript examples for String:indexOf

Description

Get all indexes of a pattern in a string

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/* w w  w .jav a  2 s .co  m*/
var str = "abcdab";
var re = /a/g;
var matches;
var indexes = [];
while (matches = re.exec(str)) {
    indexes.push(matches.index);
}
console.log(indexes);
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials