javascript - How to replace repeating characters with the same number of asterisks? -
i want replace repeated characters same number of asterisks (*).
for example:
replaceletters("keviin");
should return:
kev**n
and:
replaceletters("haaa");
should return:
h***
how can go doing this?
if use following code replace repeating characters, replaces whole repetition 1 asterisk instead of same number of asterisks repeating characters.
function replaceletters(s) { s.replace(/([^])\1+/g, '*'); }
.replace()
allows function passed second argument. argument callback function passed matched string. once have access matched string, there number of ways can replace matched string same number of asterisks.
for example, turn string array, use map replace each array item asterisk , join array string.
function replaceletters(s) { return s.replace(/([^])\1+/g,function(m) { return m.split('').map(function(){ return '*' }).join(''); }); }
or replace every character in matched string asterisk.
function replaceletters(s) { return s.replace(/([^])\1+/g,function(m) { return m.replace(/[^]/g,'*'); }); }
there many ways go this, depends on how want accomplish task.
Comments
Post a Comment