Search code examples
c#javascriptcodemirrorjshint

Code mirror get uncomment code only


I have implemented the latest code mirror in our asp.net application. the editor allows to type javascript which has commented code and un commented code.

I want to know whether all the code written in the editor is commented or not.

Can anyone please advice.


Solution

  • You can fetch the document text, than pass it through a regex, filtering out all the comments:

    var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
        lineNumbers: true
    });
    
    var out = document.getElementById('out');
    out.textContent = editor.getValue()
            .replace(/(?:\/\*(?:[\s\S]*?)\*\/)|(?:[\s;]+\/\/(?:.*)$)/gm, '');
    

    jsfiddle icon Demo