2010-11-26 140 views
11

我很惊讶正则表达式我认为我是阅读障碍,当涉及到这些可怕的代码位......无论如何,必须有一个更简单的方法来做到这一点 - (即列出一组替换实例一行),任何人?提前致谢。多个正则表达式替换

function clean(string) { 
    string = string.replace(/\@[email protected]/g, '').replace(/}/g, '@[email protected]'); 
    string = string.replace(/\@[email protected]/g, '').replace(/{/g, '@[email protected]'); 
    string = string.replace(/\@[email protected]/g, '').replace(/\"/g, '@[email protected]'); 
    string = string.replace(/\@[email protected]/g, '').replace(/\:/g, '@[email protected]'); 
    string = string.replace(/\@[email protected]/g, '').replace(/\,/g, '@[email protected]'); 
    return string; 
} 
+0

您能否提供一些示例输入? – Shekhar 2010-11-26 13:09:18

+1

你究竟在做什么? – Gumbo 2010-11-26 14:14:20

回答

10

您可以定义任何一个通用的功能,这将使意义,如果你可以在你的代码的多个部分重用,从而使其干燥。如果你没有理由去定义一个通用的,那么我只会压缩​​那些清理序列并保留其他替换的部分。

function clean(string) { 
    string = string.replace(/\@[email protected]|\@[email protected]|\@[email protected]|\@[email protected]|\@[email protected]/g, '') 
     .replace(/}/g, '@[email protected]').replace(/{/g, '@[email protected]') 
     .replace(/\"/g, '@[email protected]').replace(/\:/g, '@[email protected]') 
     .replace(/\,/g, '@[email protected]'); 
    return string; 
} 

但要注意,在替换的顺序在这段代码虽然似乎他们可能不会影响结果是改变了它..。

0

你可以做这样的:

function clean(str) { 
    var expressions = { 
     '@[email protected]': '', 
     '}':  '@[email protected]', 
     // ... 
    }; 

    for (var key in expressions) { 
     if (expressions.hasOwnProperty(key)) { 
      str = str.replace(new RegExp(key, 'g'), expressions[key]); 
     } 
    } 

    return str; 
} 

请记住,对象属性的顺序是不能可靠取得的(但大多数实现中定义的顺序返回)。如果您需要确保特定订单,您可能需要多个此类结构。

0

您可以将它们全部按顺序链接起来。

function clean(string) { 
    return string.replace(/\@[email protected]/g, '').replace(/}/g, '@[email protected]') 
       .replace(/\@[email protected]/g, '').replace(/{/g, '@[email protected]') 
       .replace(/\@[email protected]/g, '').replace(/\"/g, '@[email protected]') 
       .replace(/\@[email protected]/g, '').replace(/\:/g, '@[email protected]') 
       .replace(/\@[email protected]/g, '').replace(/\,/g, '@[email protected]'); 
} 
27

您可以使用功能替换。对于每场比赛,该功能决定应该用什么替换。

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 "@[email protected]"; 
      case '}': return "@[email protected]"; 
      case '"': return "@[email protected]"; 
      case ':': return "@[email protected]"; 
      case ',': return "@[email protected]"; 
     } 
    }); 
} 
0

...必须有做 这个 - (即列出一组一行替换 实例)...

百胜,API-一个更简单的方法先思考。怎么样...?

var clean = multiReplacer({ 
    "@[email protected]": "", 
    "@[email protected]": "", 
    "@[email protected]": "", 
    "@[email protected]": "", 
    "@[email protected]": "", 
    "}": "@[email protected]", 
    "{": "@[email protected]", 
    "\\": "@[email protected]", 
    ":": "@[email protected]", 
    ",": "@[email protected]" 
}); 

水暖:

// 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); 
    }; 
}