I am trying to replace all occurences of 'S' (capital S) with '.sss' :
For example the following line doesn't even contain a capital S in it and it still replaces a lot :
"MM/dd/yyyy EEEE hh:mm aa".replace(new RegExp('S*', 'g'),'.sss')
The result is :
".sssM.sssM.sss/.sssd.sssd.sss/.sssy.sssy.sssy.sssy.sss .sssE.sssE.sssE.sssE.sss .sssh.sssh.sss:.sssm.sssm.sss .sssa.sssa.sss"
Don't have any Ideea ..
(to test this example just copy and paste it in chrome or firefox dev tools on any page and run it)
That is because
* matches 0 or more, so you are also matching the occurence of 0 "S", means it matches every position, where "S" is not ahead, or behind (see it on Regexr). Change it to
see it on Regexr, or even only "S".
+ Quantifier matches one or more.
because * matches zero or more times the previous character.
"+" matches the character at least 1 time but also more.
var res = str.replace(/S/g,".sss");
Try this. Use this link