working on a project in which user writes some javascript code, i just need the syntax error if exists in user's code but don't want to execute the code..Is there any library exists or solution exists in c#?
-
2Have you done a google search? Have you tried anything out? Unfortunately this isn't a question for Stack OverflowCallum Linington– Callum Linington2016-06-27 09:55:24 +00:00Commented Jun 27, 2016 at 9:55
-
Can this help you?Matteo Umili– Matteo Umili2016-06-27 09:57:28 +00:00Commented Jun 27, 2016 at 9:57
-
tried alot but could'nt find the exact solution.Syed Kamran Ahmed– Syed Kamran Ahmed2016-06-27 09:57:39 +00:00Commented Jun 27, 2016 at 9:57
-
Thanks codroipa,but this is executing the script i just want to get syntax error of codeSyed Kamran Ahmed– Syed Kamran Ahmed2016-06-27 09:59:39 +00:00Commented Jun 27, 2016 at 9:59
Add a comment
|
1 Answer
You can use Jint. It can parse and execute JavaScript. We are interested in parsing, so we can do:
static void Main(string[] args)
{
try
{
var validCode = "alert('Hello World!');";
var invalidCode = "alert['Hello World!');";
var jsParser = new JavaScriptParser();
var result = jsParser.Parse(validCode);
result = jsParser.Parse(invalidCode); //Will throw...
}
catch (Exception ex)
{
//Invalid input!!
}
}