Greediness behaving differently in JavaScript?

前端 未结 3 1803
天涯浪人
天涯浪人 2020-12-15 04:32

There was this question which made me realise that greediness of quantifiers is not always the same in certain regex engines. Taking the regex from that question and modifyi

3条回答
  •  再見小時候
    2020-12-15 04:54

    This is not answering why grediness is behaving differently in Javascript but it shows that this is not a bug and it was tested to behave so. I will take as example google's v8 engine. I found a similar example in their tests.

    /test/mjsunit/third_party/regexp-pcre.js:

    line 1085: res[1006] = /([a]*?)*/;
    line 4822: assertToStringEquals("aaaa,a", res[1006].exec("aaaa "), 3176);
    

    https://code.google.com/p/v8/source/browse/trunk/test/mjsunit/third_party/regexp-pcre.js#1085

    This code works well for Javascript http://regex101.com/r/nD0uG8 but it does not have the same output for PCRE php and python.

    UPDATE: About your question:

    It seems that JavaScript's regex engine is interpreting the second * to convert .*? from lazy to greedy

    https://code.google.com/p/v8/source/browse/trunk/src/parser.cc#5157

    RegExpQuantifier::QuantifierType quantifier_type = RegExpQuantifier::GREEDY;
    if (current() == '?') {
        quantifier_type = RegExpQuantifier::NON_GREEDY;
        Advance();
    } else if (FLAG_regexp_possessive_quantifier && current() == '+') {
        // FLAG_regexp_possessive_quantifier is a debug-only flag.
        quantifier_type = RegExpQuantifier::POSSESSIVE;
        Advance();
    }
    

提交回复
热议问题