Javascript examples for String Operation:String Replace
Replace dash char using regex and string replace() function
<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>