hkernbach hkernbach - 1 year ago 50
Javascript Question

How do I get value from ACE editor without comments?

Is it possible to get the value of an ace editor instance without the comments (single & multi row)? The comments are identified by the span class 'ace_comment', but the only function I found to extract the data is getValue().

Easy Example:

console.log("Hello World") //This is a comment.

What I get:
Output: 'console.log("Hello World") //This is a comment.'

What I want:
Output: 'console.log("Hello World")'

Extended Example (Multi-row + '//' and '/* */' comments):

*/ This is
a comment */ console.log("this is not a comment") // comment again


You can use regular expressions to remove comments:

var string = 'console.log("Hello World") //This is a comment. \n' + 
             'hello("foo"); /* This is also a comment. */';
string = string.replace(/\s*\/\/.*\n/g, '\n').replace(/\s*\/\*.*?\*\//g, '');
document.body.innerHTML = '<pre>' + string + '</pre>';