2010-11-26 8 views
11

मुझे रेगेक्स द्वारा boggled है, मुझे लगता है कि जब मैं कोड के इन भयानक बिट्स की बात करता हूं तो मैं डिस्लेक्सिक हूं .. वैसे भी, ऐसा करने का एक आसान तरीका होना चाहिए- (यानी। उदाहरणों को प्रतिस्थापित करने का एक सेट सूचीबद्ध करें एक लाइन), कोई भी? अग्रिम में धन्यवाद।एकाधिक रेगेक्स

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

आप या तो एक सामान्य समारोह है, जो मतलब होगा अगर आप अपने कोड के अधिक भागों में यह पुन: उपयोग कर सकते हैं, इस प्रकार यह सूखी बनाने परिभाषित कर सकते हैं। यदि आपके पास एक सामान्य व्यक्ति को परिभाषित करने का कोई कारण नहीं है, तो मैं केवल उस भाग को संकुचित करता हूं जो अनुक्रमों को साफ़ करता है और अन्य प्रतिस्थापन को छोड़ देता है।

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

@ फ्रेड गंडट मैं प्रतिलिपि कॉलबैक में कैप्चर-ग्रुप का उपयोग कर रहा हूं, इसलिए इसके बजाय गैर-कैप्चरिंग समूहों का उपयोग करने की टिप्पणी की आवश्यकता नहीं है। मैं इस बदलाव को वापस कर रहा हूं। –

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