2010-11-26 139 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); 
    }; 
}