Replacing multiple spaces using script

Previously a button script was provided to clean generic file names and as an example has the following to replace _ with nothing.

   tmp = tmp.replace(/_/gm, '');

I want to know if there are multiple characters in succession, what do we use to replace it with a single occurence.

e.g. If I want to replace multiple spaces with a single space, how do I achieve it in the following ? (e.g. three spaces are replaced with one in example below but it will not work with lesser or more spaces)

tmp = tmp.replace(/\ \ \ /gm, '\ ');

Same with dot. If I have three dots, I can do following to replace it with one dot. But if the number of dots varies and I want one dot for more than one dots in succession, how do I achieve it ?

tmp = tmp.replace(/\.\.\./gm, '\.');

A + character means match the previous token any number of times. So e.g. \.+ will match any number of dots.

1 Like