Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 316
hkernbach Avatar asked Jul 15 '26 22:07

hkernbach


1 Answers

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*\/\*[\s\S]*?\*\//g, '');
document.body.innerHTML = '<pre>' + string + '</pre>';
like image 125
jcubic Avatar answered Jul 18 '26 13:07

jcubic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!