2011-10-29 9 views
5

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

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

इसे नियमित अभिव्यक्ति शाब्दिक द्वारा भ्रमित किया जा सकता है। तो मैं जावास्क्रिप्ट की एक स्ट्रिंग में नियमित अभिव्यक्ति अक्षर का पता लगाने में कुछ मदद की उम्मीद कर रहा हूं, इसलिए मैं उन्हें उचित तरीके से संभाल सकता हूं।

कुछ इस तरह:

function getRegExpLiterals (stringOfJavascriptCode) { 
    var output = []; 
    // todo! 
    return output; 
} 

var jsString = "var regexp1 = /abcd/g, regexp1 = /efg/;" 
console.log (getRegExpLiterals (jsString)); 

// should print: 
// [{startIndex: 13, length: 7}, {startIndex: 32, length: 5}] 
+0

किसी भी नियमित अभिव्यक्ति शाब्दिक पर शुरू? यदि आप बस // में क्या चाहते हैं जो करना आसान है। – FailedDev

+0

मुझे यह सुनिश्चित करने की ज़रूरत है कि यह एक रेगेक्स शाब्दिक है, इसलिए केवल स्लेश की तलाश करना ऐसा नहीं होगा। – rob

उत्तर

5

es5-lexer एक जे एस lexer विभाजन भाव से जे एस कोड में नियमित अभिव्यक्ति भेद करने के लिए एक बहुत ही सटीक अनुमान का उपयोग करता है, और यह भी कहा कि एक टोकन स्तर परिवर्तन है कि आप बनाने के लिए उपयोग कर सकते हैं प्रदान करता है सुनिश्चित करें कि परिणामस्वरूप प्रोग्राम को एक पूर्ण जेएस पार्सर द्वारा लेक्सर के रूप में उसी तरह व्याख्या किया जाएगा।

बिट निर्धारित करता है कि एक / एक रेगुलर एक्सप्रेशन है शुरू होता है guess_is_regexp.js में है कि क्या और परीक्षण scanner_test.js line 401

var REGEXP_PRECEDER_TOKEN_RE = new RegExp(
    "^(?:" // Match the whole tokens below 
    + "break" 
    + "|case" 
    + "|continue" 
    + "|delete" 
    + "|do" 
    + "|else" 
    + "|finally" 
    + "|in" 
    + "|instanceof" 
    + "|return" 
    + "|throw" 
    + "|try" 
    + "|typeof" 
    + "|void" 
    // Binary operators which cannot be followed by a division operator. 
    + "|[+]" // Match + but not ++. += is handled below. 
    + "|-" // Match - but not --. -= is handled below. 
    + "|[.]" // Match . but not a number with a trailing decimal. 
    + "|[/]" // Match /, but not a regexp. /= is handled below. 
    + "|," // Second binary operand cannot start a division. 
    + "|[*]" // Ditto binary operand. 
    + ")$" 
    // Or match a token that ends with one of the characters below to match 
    // a variety of punctuation tokens. 
    // Some of the single char tokens could go above, but putting them below 
    // allows closure-compiler's regex optimizer to do a better job. 
    // The right column explains why the terminal character to the left can only 
    // precede a regexp. 
    + "|[" 
    + "!" // !   prefix operator operand cannot start with a division 
    + "%" // %   second binary operand cannot start with a division 
    + "&" // &, &&  ditto binary operand 
    + "(" // (   expression cannot start with a division 
    + ":" // :   property value, labelled statement, and operand of ?: 
      //    cannot start with a division 
    + ";" // ;   statement & for condition cannot start with division 
    + "<" // <, <<, << ditto binary operand 
    // !=, !==, %=, &&=, &=, *=, +=, -=, /=, <<=, <=, =, ==, ===, >=, >>=, >>>=, 
    // ^=, |=, ||= 
    // All are binary operands (assignment ops or comparisons) whose right 
    // operand cannot start with a division operator 
    + "=" 
    + ">" // >, >>, >>> ditto binary operand 
    + "?" // ?   expression in ?: cannot start with a division operator 
    + "[" // [   first array value & key expression cannot start with 
      //    a division 
    + "^" //^   ditto binary operand 
    + "{" // {   statement in block and object property key cannot start 
      //    with a division 
    + "|" // |, ||  ditto binary operand 
    + "}" // }   PROBLEMATIC: could be an object literal divided or 
      //    a block. More likely to be start of a statement after 
      //    a block which cannot start with a /. 
    + "~" // ~   ditto binary operand 
    + "]$" 
    // The exclusion of ++ and -- from the above is also problematic. 
    // Both are prefix and postfix operators. 
    // Given that there is rarely a good reason to increment a regular expression 
    // and good reason to have a post-increment operator as the left operand of 
    // a division (x++/y) this pattern treats ++ and -- as division preceders. 
); 
+0

धन्यवाद माइक, मैं भविष्य में पूर्ण लेक्सर के लिए उपयोग कर सकता हूं, यह काम का एक प्रभावशाली टुकड़ा है (जैसा कि आपने लिखा है कि प्रेटिफायर है, और जिसे मैंने बड़े पैमाने पर उपयोग किया है) – rob

+0

@rob, आपका स्वागत है। हैप्पी लेक्सिंग। –

संबंधित मुद्दे