4

I have a Java String that contains javascript code, and i need to extract all the javascript vars' names.

So, for the following javasctipt:

var x;
var a,b,c,d;
var y = "wow";
var z = y + 'x';

I need to get "x,a,b,c,d,y,z" as a result.

I dont need to get their values, just their names.

poitroae
  • 21,129
  • 10
  • 63
  • 81

3 Answers3

4

Well you can try and get the bindings that the execution of the script creates:

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine se = mgr.getEngineByName("JavaScript");

try {
    se.eval("var x;var a,b,c,d;var y = \"wow\";var z = y+'x';");
    Bindings bindings = se.getBindings(ScriptContext.ENGINE_SCOPE);
    System.out.println(bindings.keySet());
}
catch (ScriptException e) {
    //e.printStackTrace();
}

this prints [d, b, c, println, a, context, z, y, print, x]

as you see some extra bindings are defined: context, print and println

and here we filter them out

Set<String> keySet = bindings.keySet();
keySet.removeAll(Arrays.asList("context", "print", "println"));
System.out.println(keySet);

this prints [d, b, c, a, z, y, x]

Liviu T.
  • 23,584
  • 10
  • 62
  • 58
1

Something like the following:

List<String> vars = new ArrayList<String>();

Pattern p = Pattern.compile("^\\s*var\\s+(.*?)\\s*;?$");

BifferedReader reader = .... // create the reader
String line = null;
while ((line = reader.readLine()) != null) {
    Matcher m = p.matcher(line);
    if (m.find()) {
        vars.addAll(Arrays.asList(m.group(1).split("\\s*,\\s*")));       
    }
}

Please note that I wrote this code without IDE and have never compiled and ran it. So, sorry for possible bugs but I thing it is readable enough and can be a good starting point for you.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

I'm not sure whether this would be too convoluted, but I would probably get the BNF for Javascript (see SO: Repository of BNF Grammars?) and then use something like ANTLR to create a parser for Javascript, which you can then use to extract all the variables based on the Javascript source code. See also SO: ANTLR Tutorials.

Community
  • 1
  • 1
beny23
  • 34,390
  • 5
  • 82
  • 85