user2349035
user2349035

Reputation: 47

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

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

Answers (2)

Anirudha
Anirudha

Reputation: 32797

If you want to to have a single regex,you can use

input.replace(/([^.]|^)(?:([.])[.]|([.]{3})[.]+)(?![.])/g, "$1$2$3");

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

Try

fieldval = fieldval.replace(
    /\.{2,}/g, 
    function(val){ 
        return val.length == 2 ? '.' : '...';
    }
);

Demo: Fiddle

Upvotes: 2

Related Questions