Replace double period to single and if 4 or more replace to three with String replace and regex - Javascript String Operation

Javascript examples for String Operation:String Replace

Description

Replace double period to single and if 4 or more replace to three with String replace and regex

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>SO - 17271625</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//  w ww . j  a  v  a 2 s  . c  om
var fieldval = 'testing.. with ... and more .... and .........';
fieldval = fieldval.replace(
    /\.{2,}/g,
    function(val){
        return val.length == 2 ? '.' : '...';
    }
);
console.log(fieldval)
    }

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

Related Tutorials