2

I want to detect if a string is a valid javasript code for pass it for the evaluation of rhino engine, now i'm assuming that the string is a javascript script with this simple statement :

if(jsString.trim().endsWith(";")) {
//Evaluate with Rhino
}

Is there a better way for figure out if a string is a valid JS script ?

What is the best way ?

4
  • What do you mean by "valid JS script"? Pass it to Rhino and if it works, it's valid. Commented Oct 13, 2011 at 11:01
  • Cant you just pass it in and catch any exception if it fails and then handle it from there? Commented Oct 13, 2011 at 11:05
  • See also the ScriptEngine. Example here. Commented Oct 13, 2011 at 11:19
  • jsString.trim().endsWith(";") BTW - AFAIU a single line script does not need a trailing ;. Commented Oct 13, 2011 at 11:22

1 Answer 1

1

You could utilise something like: http://code.google.com/p/jslint4java/

In any case, you want to run the string through JSLint.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.