Replace dash char using regex and string replace() function - Javascript String Operation

Javascript examples for String Operation:String Replace

Description

Replace dash char using regex and string replace() function

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() {/*  www  . j  av a2 s  . c  om*/
var string = "11111-22222-333---444---55--5566--6-----7-";
console.log(string.replace(/-+/g, function(str, p1, offset) {
    return offset.lastIndexOf("-") == p1 ? "" : "-";
}));
    });

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

Related Tutorials