Array natural sort method, sort string and filter number with regex - Javascript RegExp

Javascript examples for RegExp:Match Number

Description

Array natural sort method, sort string and filter number with regex

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <script>
var arr = ['columnNumber6','columnNumber1','coulmnNumber11','columnNumber7','columnNumber12'];
arr = arr.sort(function(a, b) {
  return  +/\d+/.exec(a)[0] > +/\d+/.exec(b)[0];
});/*from ww  w. j a  v a 2 s .  c o  m*/
console.log(arr);

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

Related Tutorials