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

Regexp: \r and \n not handled properly

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 8u111
    • core-libs
    • None

      Hi

      I stumbled upon a minor discrepancy between Nashorn and other
      JavaScript engines when doing regexp matching with the multiline flag
      enabled.

      Nashorn
      ```
      $ jjs -version
      nashorn 1.8.0_111
      jjs> String.prototype.replace.apply("a\nb\rc\n\rd\r\ne", [/^(.*)/gm,"*$1"]);
      *a
      *c
      **
      *e
      ```

      Notice that the characters 'b' and 'd' are gone from the output.

      Node:
      ```
      $ node --version
      v5.5.0
      $ node
      String.prototype.replace.apply("a\nb\rc\n\rd\r\ne", [/^(.*)/gm,"*$1"]);
      '*a\n*b\r*c\n*\r*d\r*\n*e'
      ```

      Firefox & Chrome console:
      ```
      String.prototype.replace.apply("a\nb\rc\n\rd\r\ne", [/^(.*)/gm,"*$1"]);
      "*a
      *b
      *c
      *
      *d
      *
      *e"
      ```

      Notice that the characters 'b' and 'd' are in the output.

      -

      I suspect the cause is that nashorn does treat the character after
      '\r' as being at the start of a line.

      -
      Esben

            hannesw Hannes Wallnoefer
            jlaskey Jim Laskey
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: