hkernbach
hkernbach

Reputation: 206

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

Upvotes: 3

Views: 869

Answers (2)

jcubic
jcubic

Reputation: 66488

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>';

Upvotes: 4

Atanas Kovachev
Atanas Kovachev

Reputation: 431

A simpler solution will be. To just trim the //Comment like this

    var str = 'console.log("HelloWorld")//This is a comment';
        str = str.split('//');

    //Just to print out
    document.body.innerHTML = '<pre>You provide > '+ str[0]+'//'+str[1] +'</pre><pre>Output > ' + str[0] + '</pre>';

By doing this you're spliting the the entire string by the '//' split returns an array that will have [0] = console.log('HelloWorld'); and the [1] = 'Comment like this'. This is the simplest solution i can think of. jcubic. Is basically doing the same by using a regex. Good look!

Upvotes: 0

Related Questions