I am boggled by regex I think I’m dyslexic when it comes to these horrible bits of code.. anyway, there must be an easier way to do this- (ie. list a set of replace instances in one line), anyone? Thanks in advance.
function clean(string) {
string = string.replace(/\@~rb~@/g, '').replace(/}/g, '@~rb~@');
string = string.replace(/\@~lb~@/g, '').replace(/{/g, '@~lb~@');
string = string.replace(/\@~qu~@/g, '').replace(/\"/g, '@~qu~@');
string = string.replace(/\@~cn~@/g, '').replace(/\:/g, '@~cn~@');
string = string.replace(/\@-cm-@/g, '').replace(/\,/g, '@-cm-@');
return string;
}
You can define either a generic function, which would make sense if you can reuse it in more parts of your code, thus making it DRY. If you don’t have reason to define a generic one, I would compress only the part which cleans sequences and leave the other replaces as they are.
But be careful, the order of the replacements were changed it in this code.. although it seems they might not affect the result.