find ALL instances of a regex inside a string - Javascript String Operation

Javascript examples for String Operation:String Search

Description

find ALL instances of a regex inside 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(){//ww w . j  av a  2 s.  c  o m
var input = "TEST 65-K-N-P|2.1-K1-ON-PEAK|164|123-KH-OFF-TEST|27-K1";
var words = input.split("|");
var ids = [];
for( var i = 0; i < words.length; ++i) {
  var w = words[i];
  var digits = w.match(/[0-9.]+/);
  ids.push(digits[0]);
}
console.log(ids);
    }

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

Related Tutorials