Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8137240

Negative lookahead in RegEx breaks backreference

    XMLWordPrintable

Details

    • 8
    • b125
    • x86
    • other

    Backports

      Description

        FULL PRODUCT VERSION :
        java version "1.8.0_60"
        Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
        Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Darwin 14.5.0 Darwin Kernel Version 14.5.0: Wed Jul 29 02:26:53 PDT 2015; root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64 i386

        A DESCRIPTION OF THE PROBLEM :
        In the Nashorn engine, using a JavaScript RegEx containing a matching group followed by a negative lookahead followed by a backreference to that matching group does not work as expected.
        The backreference appears to refer to the negative lookahead instead of the matching group.
        Since a negative lookahead is zero-length, this will cause the backreference to match anything.
        This can be seen, for example, when using the RegEx for replacement, using JavaScript's String.prototype.replace function.

        Example JavaScript code:

        'aa'.replace(/(a)(?!b)\1/gm, 'c');

        This returns

        cc

        when one would expect only

        c

        I don't know whether this only applies to negative lookaheads or lookarounds in general, but it certainly does apply to negative lookaheads.
        Running the exact same code in the browser console of Chrome/Firefox/Safari/Opera produces the expected result.

        Related question on StackOverflow:

        https://stackoverflow.com/q/32480370

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Just run the attached code.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        A console output of:

        c
        ACTUAL -
        A console output of:

        cc

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import javax.script.Invocable;
        import javax.script.ScriptEngine;
        import javax.script.ScriptEngineManager;

        class Test
        {
            public static void main(String[] args) throws Exception
            {
                ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript");
                js.eval("function x(s){return s.replace(/(a)(?!b)\1/gm, 'c');}");
                System.out.println(String.valueOf(((Invocable)js).invokeFunction("x", "aa")));
            }
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        None, apart from installing a separate program to parse and run JavaScript, and calling said program using Runtime.getRuntime().exec().

        Attachments

          Issue Links

            Activity

              People

                hannesw Hannes Wallnoefer
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: