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 instanc
You could do it like this:
function clean(str) {
var expressions = {
'@~rb~@': '',
'}': '@~rb~@',
// ...
};
for (var key in expressions) {
if (expressions.hasOwnProperty(key)) {
str = str.replace(new RegExp(key, 'g'), expressions[key]);
}
}
return str;
}
Keep in mind that the order of object properties is not reliably determinable (but most implementations will return them in order of definition). You will probably need multiple constructs like this if you need to ensure a specific order.
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.
function clean(string) {
string = string.replace(/\@~rb~@|\@~lb~@|\@~qu~@|\@~cn~@|\@-cm-@/g, '')
.replace(/}/g, '@~rb~@').replace(/{/g, '@~lb~@')
.replace(/\"/g, '@~qu~@').replace(/\:/g, '@~cn~@')
.replace(/\,/g, '@-cm-@');
return string;
}
But be careful, the order of the replacements were changed it in this code.. although it seems they might not affect the result.
You can just chain them all in order.
function clean(string) {
return string.replace(/\@~rb~@/g, '').replace(/}/g, '@~rb~@')
.replace(/\@~lb~@/g, '').replace(/{/g, '@~lb~@')
.replace(/\@~qu~@/g, '').replace(/\"/g, '@~qu~@')
.replace(/\@~cn~@/g, '').replace(/\:/g, '@~cn~@')
.replace(/\@-cm-@/g, '').replace(/\,/g, '@-cm-@');
}
You could use a function replacement. For each match, the function decides what it should be replaced with.
function clean(string) {
// All your regexps combined into one:
var re = /@(~lb~|~rb~|~qu~|~cn~|-cm-)@|([{}":,])/g;
return string.replace(re, function(match,tag,char) {
// The arguments are:
// 1: The whole match (string)
// 2..n+1: The captures (string or undefined)
// n+2: Starting position of match (0 = start)
// n+3: The subject string.
// (n = number of capture groups)
if (tag !== undefined) {
// We matched a tag. Replace with an empty string
return "";
}
// Otherwise we matched a char. Replace with corresponding tag.
switch (char) {
case '{': return "@~lb~@";
case '}': return "@~rb~@";
case '"': return "@~qu~@";
case ':': return "@~cn~@";
case ',': return "@-cm-@";
}
});
}
...there must be an easier way to do this- (ie. list a set of replace instances in one line)...
Yum, API-first thinking. How about...?
var clean = multiReplacer({
"@~rb~@": "",
"@~lb~@": "",
"@~qu~@": "",
"@~cn~@": "",
"@-cm-@": "",
"}": "@~rb~@",
"{": "@~lb~@",
"\\": "@~qu~@",
":": "@~cn~@",
",": "@-cm-@"
});
Plumbing:
// From http://simonwillison.net/2006/Jan/20/escape/
RegExp.escape = function(text)
{
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};
function multiReplacer(replacements)
{
var regExpParts = [];
for (prop in replacements)
{
if (replacements.hasOwnProperty(prop))
{
regExpParts.push(RegExp.escape(prop));
}
}
var regExp = new RegExp(regExpParts.join("|"), 'g');
var replacer = function(match)
{
return replacements[match];
};
return function(text)
{
return text.replace(regExp, replacer);
};
}