Filter Spaces around all hyphens in a string without double-up - Javascript String

Javascript examples for String:replace

Description

Filter Spaces around all hyphens in a string without double-up

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <script>
var instr = "jaguar-leopard\n"+
    "jaguar -leopard\n"+
    "jaguar- leopard\n"+
    "jaguar - leopard";
instr=instr.replace(/(\S)-/g,'$1 -').replace(/-(?=\S)/g,'- ');
console.log(instr);

      </script>  
   </body>/*from  w  ww  .j a va2 s  . c  o m*/
</html>

Related Tutorials