2010-11-26 17 views
11

私は正規表現に悩まされています。これらの恐ろしいコードになると、私は失読症だと思います..とにかく、これを行うための簡単な方法が必要です。(つまり、 1行)、誰ですか?前もって感謝します。複数の正規表現を置き換えます

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

+1

実際に何をしようとしていますか? – Gumbo

答えて

10

あなたはので、それはDRYなって、あなたのコードのより多くの部分でそれを再利用することができれば理にかなって、一般的な機能、のいずれかを定義することができます。一般的なものを定義する理由がない場合は、シーケンスを消去し、そのまま残りの部分を残す部分だけを圧縮します。

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

すべてを順番にチェーンできます。

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

@Fred Gandt私はリプレースコールバックでキャプチャグループを使用しているので、キャプチャしていないグループの使用に関するコメントは必要ありません。私はこの変更を元に戻しています。 –

0

... this-行うための簡単な方法がなければならない(すなわち。1行に置き換える インスタンスのセットをリスト)...

ヤム、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); 
    }; 
} 
関連する問題