![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Suppose you have some javascript statements in a string. |
|
Can you determine whether the entire string is syntactically valid, and if so, the starting position of the last statement? In other words, function lastStatementPos(code) { // returns the starting position within code of the last // javascript statement, and -1 if code is not syntactiaclly // valid. This came up in a different context today, and I thought it would make an interesting exercise. |
#3
| |||
| |||
|
|
Suppose you have some javascript statements in a string. Can you determine whether the entire string is syntactically valid, and if so, the starting position of the last statement? In other words, function lastStatementPos(code) { // returns the starting position within code of the last // javascript statement, and -1 if code is not syntactiaclly // valid. This came up in a different context today, and I thought it would make an interesting exercise. |
#4
| |||
| |||
|
|
Suppose you have some javascript statements in a string. Can you determine whether the entire string is syntactically valid, and if so, the starting position of the last statement? |
|
In other words, function lastStatementPos(code) { // returns the starting position within code of the last // javascript statement, and -1 if code is not syntactiaclly // valid. This came up in a different context today, and I thought it would make an interesting exercise. |
#5
| |||
| |||
|
|
Suppose you have some javascript statements in a string. Can you determine whether the entire string is syntactically valid, and if so, the starting position of the last statement? In other words, function lastStatementPos(code) { // returns the starting position within code of the last // javascript statement, and -1 if code is not syntactiaclly // valid. |
#6
| |||
| |||
|
|
Suppose you have some javascript statements in a string. |
|
Can you determine whether the entire string is syntactically valid, |
|
and if so, the starting position of the last statement? |
#7
| |||
| |||
|
|
Csaba *Gabor wrote on 03 nov 2009 in comp.lang.javascript: Suppose you have some javascript statements in a string. Can you determine whether the entire string is syntactically valid, and if so, the starting position of the last statement? In other words, function lastStatementPos(code) { * // returns the starting position within code of the last * // javascript statement, and -1 if code is not syntactiaclly * // valid. This came up in a different context today, and I thought it would make an interesting exercise. A string like? str = "alert(11);{alert('alert(11); is correct Javascript');};" Would that be usefull? I don't think it would. |
|
Csaba Gabor wrote on 03 nov 2009 in comp.lang.javascript: Suppose you have some javascript statements in a string. Can you determine whether the entire string is syntactically valid, and if so, the starting position of the last statement? In other words, function lastStatementPos(code) { // returns the starting position within code of the last // javascript statement, and -1 if code is not syntactiaclly // valid. } This came up in a different context today, and I thought it would make an interesting exercise. A string like? str = "alert(11);{alert('alert(11); is correct Javascript');};" Would that be usefull? I don't think it would. |
#8
| |||
| |||
|
|
Evertjan's code = alert(11); return alert('alert(11); is correct Javascript'); |
|
The question arose in the following context: The user is asked to enter some javascript statements to affect a value. |
#9
| |||||
| |||||
|
|
The validation is fairly straightforward: |
|
function syntax_check(code) { // returns false is code is not syntactically OK |
|
// returns browser's interpretation of the code if it's OK try { |
|
var f = new Function(code); |
|
return f.toString(); } catch (err) { return false; } // syntax error } |
#10
| |||
| |||
|
|
Based on which syntax rules? *ECMAScript's, JavaScript's, JScripts or others'? And if the Function constructor is not supported, the whole thing breaks. |

![]() |
| Thread Tools | |
| Display Modes | |
| |