Replace multiple characters by one character with regex

A well written RegEx can handle your problem rather easily.
Quoting Mohit’s answer to have a startpoint:

var str="#this #is__ __#a test###__";
var formattedStr = str.replace(/[#_,]+/g, '#');
console.log( formattedStr );

Line 2:
Put in formattedStr the result of the replace method on str.
How does replace work? The first parameter is a string or a RegEx.
Note: RegExps in Javascripts are Objects of type RegExp, not strings. So writing

/yourRegex/

or

New RegExp('/yourRegex/')

is equivalent syntax.
Now let’s discuss this particular RegEx itself.
The leading and trailing slashes are used to surround the pattern, and the g at the end means “globally” – do not stop after the first match.
The square parentheses describe a set of characters who can be supplied to match the pattern, while the + sign means “1 or more of this group”.
Basically, ### will match, but also # or #####_# will, because _ and # belong to the same set.
A preferred behavior would be given by using (#|_)+
This means “# or _, then, once found one, keep looking forward for more or the chosen pattern”.
So ___ would match, as well as #### would, but __## would be 2 distinct match groups (the former being __, the latter ##).
Another problem is not knowing wheter to replace the pattern found with a _ or a #.
Luckily, using parentheses allows us to use a thing called capturing groups. You basically can store any pattern you found in temporary variabiles, that can be used in the replace pattern.
Invoking them is easy, propend $ to the position of the matched (pattern).
/(foo)textnotgetting(bar)captured(baz)/ for example would fill the capturing groups “variables” this way:

$1 = foo
$2 = bar
$3 = baz

In our case, we want to replace 1+ characters with the first occurrence only, and the + sign is not included in the parentheses!
So we can simply

str.replace("/(#|_)+/g", "$1");

In order to make it work.

Have a nice day!

Leave a Comment