![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I'm looking for a function stripEndComments(code) { // remove trailing comments and whitespace from /* the end of code, which is presumed to be valid // javascript */ ... } (...) For the rambunctious: remove trailing empty statements, too: code = "baz/* junk */+borf; fubar ; /* more junk */ ; ;; ;" => baz/* junk */+borf; fubar |
#3
| |||
| |||
|
|
Le 11/4/09 12:51 PM, Csaba Gabor a crit : I'm looking for a function stripEndComments(code) { * // remove trailing comments and whitespace from * /* the end of code, which is presumed to be valid * // javascript */ * ... } (...) For the rambunctious: remove trailing empty statements, too: code = "baz/* junk */+borf; fubar *; /* more junk */ ; ;; ;" => baz/* junk */+borf; fubar I get, Firefox.3 : * * *baz + borf; * * *fubar; IE.5, 6 and 7 : * * *baz/* junk */+borf; fubar ; /* more junk */ ; ;; ; not yet finished ? |
#4
| |||
| |||
|
|
I'm looking for a function stripEndComments(code) { // remove trailing comments and whitespace from /* the end of code, which is presumed to be valid // javascript */ ... } My previous post at ... may amount to more than just an exercise, so I am slicing off part of it into an independent exercise (and this one IS just an exercise). Assume the use of the function function checkSyntax(code) { // returns false if code is not syntactically OK // returns browser's (string) interpretation of the code if it's OK, // encapsulated in an anonymous function try { var f = new Function(code); return f.toString(); } catch (err) { return false; } } // syntax error Some examples: foo + bar // two comments /* or one? *// => foo + bar "Foo" + "bar" /* three */ // lines // of comments /* should all be /* stripped off *//// => "Foo" + "bar" For the rambunctious: remove trailing empty statements, too: code = "baz/* junk */+borf; fubar ; /* more junk */ ; ;; ;" => baz/* junk */+borf; fubar |
#5
| |||
| |||
|
|
On Nov 4, 11:51 am, Csaba *Gabor wrote: I'm looking for a function stripEndComments(code) { * // remove trailing comments and whitespace from * /* the end of code, which is presumed to be valid * // javascript */ * ... } My previous post at ... may amount to more than just an exercise, so I am slicing off part of it into an independent exercise (and this one IS just an exercise). Assume the use of the function function checkSyntax(code) { * // returns false if code is not syntactically OK * // returns browser's (string) interpretation of the code if it's OK, * // * * encapsulated in an anonymous function * try { * * var f = new Function(code); * * return f.toString(); } * catch (err) { return false; } *} // syntax error Some examples: foo + bar // two comments /* or one? *// => foo + bar "Foo" + "bar" /* three */ * // lines // of comments /* should all be /* stripped off *//// => "Foo" + "bar" For the rambunctious: remove trailing empty statements, too: code = "baz/* junk */+borf; fubar *; /* more junk */ ; ;; ;" => baz/* junk */+borf; fubar This problem includes the problem of not reacting to comment delimiters whenever they appear in strings in the source code. For example, stripping everything from the // to the end of the line in the following would be disastrous:- |
|
var prefixToIRI = { * * 'xsd':'http://www.w3.org/2001/XMLSchema', * * 'env':'http://schemas.xmlsoap.org/soap/envelope/', * * 'xsi':'http://www.w3.org/2001/XMLSchema-instance', * * 'xml':'http://www.w3.org/XML/1998/namespace', * * 'xmlns':'http://www.w3.org/2000/xmlns' }; So for this task it seems necessary to identify the string literals within the source, which is getting towards tokenising the source. |
|
Tokenising the source was already implied in the task of verifying the syntax of the code (along with identifying comments) so maybe this stage should not be separated from the previous task if you genuinely want all the comments removed. |
#6
| |||
| |||
|
|
I'm looking for a function stripEndComments(code) { // remove trailing comments and whitespace from /* the end of code, which is presumed to be valid // javascript */ ... } My previous post at http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/2aa9a60623eb5883/ may amount to more than just an exercise, so I am slicing off part of it into an independent exercise (and this one IS just an exercise). |
#7
| |||
| |||
|
|
On Nov 4, 1:11 pm, SAM <stephanemoriaux.NoAd... (AT) wanadoo (DOT) fr.invalid wrote: Le 11/4/09 12:51 PM, Csaba Gabor a crit : I'm looking for a function stripEndComments(code) { // remove trailing comments and whitespace from /* the end of code, which is presumed to be valid // javascript */ ... } (...) For the rambunctious: remove trailing empty statements, too: code = "baz/* junk */+borf; fubar ; /* more junk */ ; ;; ;" => baz/* junk */+borf; fubar I get, Firefox.3 : baz + borf; fubar; IE.5, 6 and 7 : baz/* junk */+borf; fubar ; /* more junk */ ; ;; ; not yet finished ? Hi SAM, what you have shown is what FF/IE returns if you put the mentioned strings into a function and then do a .toString() on it. FF cleans all comments whereas IE leaves them in. |
|
However, in this exercise, I'd like to strip the TRAILING comments only, in an as browser independent fashion as possible (without recasting the code string into a different form). |
/g,''))|
The part to the right of the => above indicates the string that the desired function, stripEndComments, should return. Therefore, you can use checkSyntax as a false vs. nonempty-string check, but I don't think you'll find the actual nonempty string return values useful for the purposes of this exercise. |
|\s+|;(?=\s*
/g,''))
#8
| |||
| |||
|
|
I'm looking for a function stripEndComments(code) { // remove trailing comments and whitespace from /* the end of code, which is presumed to be valid // javascript */ ... } |
#9
| |||
| |||
|
|
On 4 îÏÅÍ, 13:51, Csaba *Gabor <dans... (AT) gmail (DOT) com> wrote: I'm looking for a function stripEndComments(code) { š // remove trailing comments and whitespace from š /* the end of code, which is presumed to be valid š // javascript */ š ... } Something like this? code.replace(/\s*;[\s;]*/g, ';\n').replace(/^\/(?:\/[^\n]+|\*[^\/*]*?\* \/)/gm, ''); |
#10
| |||
| |||
|
|
abozhilov wrote: Csaba Gabor wrote: š // remove trailing comments and whitespace from š /* the end of code, which is presumed to be valid š // javascript */ š ... } Something like this? code.replace(/\s*;[\s;]*/g, ';\n').replace(/^\/(?:\/[^\n]+|\*[^\/*]*?\* \/)/gm, ''); You might be able to figure out a way to do this with regular expressions, but I'm thinking that it will be VERY messy |
|
because you will have to account for strings and regular expressions such as: var code = "var messy='it was windy/*sunny*'+" and */cold/*" |
![]() |
| Thread Tools | |
| Display Modes | |
| |