fork download
  1. var js = '\
  2. // /* C++ comment */ \\\n\
  3. /* C++ comment (cont) */ \n\
  4. /* t "h /* is" \n\
  5. is first C-style /* \n\
  6. // comment */ \n\
  7. and /*second C-style*/ \n\
  8. then /*last C-style*/ \n\
  9. ';
  10.  
  11. var cmtrx1 = /^(?:\/\/(?:[^\\]|\\\n?)*?\n|(?:"(?:\\[\S\s]|[^"\\])*"|'(?:\\[\S\s]|[^'\\])*'|[^\/"'\\]*))+(\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/)/;
  12.  
  13. var cmtrx2 = /(\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/)|(\/\/(?:[^\\]|\\\n?)*?)\n|(?:"(?:\\[\S\s]|[^"\\])*"|'(?:\\[\S\s]|[^'\\])*'|[\S\s][^\/"'\\]*)/g;
  14.  
  15. //
  16. print ('Script\n===========\n'+js+'\n===========\n\n');
  17.  
  18. var match;
  19. //
  20. print ("Using Regex 1\n---------------\n");
  21. if ((match=cmtrx1.exec( js )) != null)
  22. print ("Found C style comment:\n'" + match[1] + "'\n\n");
  23. //
  24. print ("Using Regex 2\n---------------\n");
  25. while ((match=cmtrx2.exec( js )) != null)
  26. {
  27. if (match[1] != undefined)
  28. {
  29. print ("- C style :\n'" + match[1] + "'\n");
  30. // break;
  31. }
  32. if (match[2] != undefined)
  33. {
  34. print ("- C++ style :\n'" + match[2] + "'\n");
  35. }
  36. }
  37.  
Success #stdin #stdout 0.32s 213952KB
stdin
Standard input is empty
stdout
Script
===========
// /* C++ comment  */      \
   /* C++ comment (cont) */  
/* t "h /* is"               
 is first C-style /*         
//  comment */               
and /*second C-style*/       
then /*last C-style*/        

===========


Using Regex 1
---------------

Found C style comment:
'/* t "h /* is"               
 is first C-style /*         
//  comment */'


Using Regex 2
---------------

- C++ style :
'// /* C++ comment  */      \
   /* C++ comment (cont) */  '

- C style :
'/* t "h /* is"               
 is first C-style /*         
//  comment */'

- C style :
'/*second C-style*/'

- C style :
'/*last C-style*/'