0

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.

Shiras
  • 115
  • 1
  • 1
  • 7
  • or is there any way to find from javascript string whether the code has commented code. – Shiras Aug 13 '13 at 10:23
  • I guess I got a solution for the same. `(\/\/.*|\/\*[\s\S]*?\*\/)` will help to find out comments in single line and multi line. remove the matching regex and check the length of the javascript string. if the length is more there is some valid code or if the length is zero the code was dead code. – Shiras Aug 14 '13 at 09:35
  • oh i just saw your comment after posting the answer... glad you got it sorted, and i hope my solution will help out anyway. – Eliran Malka Aug 14 '13 at 10:05

1 Answers1

0

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

animuson
  • 53,861
  • 28
  • 137
  • 147
Eliran Malka
  • 15,821
  • 6
  • 77
  • 100