Reputation: 47
If there is two periods(..) replace with one (.) and if there is four or more period(....) replace it with three(...).
Require both option working at same time
i try this way
var fieldval = test..
fieldval = fieldval.replace(/\.{2}/g, '.')
Output
test. (with single dot)
var fieldval = test.....
fieldval = fieldval.replace(/\.{4,}/g, '...');
output
test... (with three dot)
Both the option should work together. thanks in advance
Upvotes: 1
Views: 147
Reputation: 32797
If you want to to have a single regex,you can use
input.replace(/([^.]|^)(?:([.])[.]|([.]{3})[.]+)(?![.])/g, "$1$2$3");
Upvotes: 1
Reputation: 388316
Try
fieldval = fieldval.replace(
/\.{2,}/g,
function(val){
return val.length == 2 ? '.' : '...';
}
);
Demo: Fiddle
Upvotes: 2