Minor discepancy for multiline regexps

Esben Andreasen esben at esbena.dk
Fri Jan 6 11:27:18 UTC 2017


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


More information about the nashorn-dev mailing list