Search code examples
javajavascriptregexrhinonashorn

Nashorn and Rhino: Splitting strings by whitespace regexp


I am parsing Unix shell command outputs in a JavaScript executed by Java's script engine. I often need to split strings by a variable amount of whitespace and have decided to use a regular expression for this.

So far, in Rhino (Java 7 and prior), I could use the following to split strings by whitespace:

line.split("\\s+")

This no longer works in Nashorn (Java 8 and later), the string is simply not split at all. I have to use the following for Nashorn:

line.split(new RegExp("\\s+"))

Now - of course you might say - this does not work in Rhino. However, I need to maintain compatibility to Java 7 for at least a year to come, so I need a solution that works with both script engines.

I tried this a rather ugly workaround:

new java.lang.String(line).split("\\s+")

Surprisingly with no luck in Nashorn. Apparently, it somehow forces the creation of its NativeString counterpart for strings.

Is there a nice solution to split by a variable amount of whitespace, one that works with both script engines? Or do you have to go the "dirty" way - ie determine the script engine from within the script (if that is even possible)?


Solution

  • How about using a regex literal?

    line.split(/\\s+/)
    

    Should work identically in every JS engine.