First, since you already did so inside of your two last expressions, with the same replacement:
article = article.replace(/\r?\n|\r/g,"")
article = article.replace(/\$|\#|\[|\]/g, "")
I'm puzzled why you didn't simply put both in a unique regexp:
article = article.replace(/\r?\n|\r|\$|\#|\[|\]/g, "")
Then to integrate with the 1st one, you might choose to:
join the two distincts replacements in a single line:
var article = a.replace(/ |\./g, "_").replace(/\r?\n|\r|\$|\#|\[|\]/g, "")
or use a map approach, either suche the one pointed by @greybeard's link, or like this way (even if it might look a bit too sohpisticated for only two cases):
var replacements = new Map([
[/ |\./g, '_'],
[/\r?\n|\r|\$|\#|\[|\]/g, '']
]),
article = a;
replacements.forEach(function(value, key){
article = article.replace(key, value);
});
The most interesting aspect in this latter solution is that it may be easily expanded if more replacements are needed.
EDIT following a good suggestion from @Niet the Dark Absol.
As soon as there are several unique characters to look for, with the same replacement, this kind of regexp /(a|b|c)/
can be replaced by /[abc]/
, which is both simpler and more efficient!
Any of the above proposed solutions can be improved this way, so the latter one becomes:
var replacements = new Map([
[/[ .]/g, '_'],
[/[\r\n$#[\]]/g, '']
]),
article = a;
replacements.forEach(function(value, key){
article = article.replace(key, value);
});