lib/decojs.js
function decojs(str) {
	var i
		, curChar, nextChar, lastNoSpaceChar
		, inString        = false
		, inComment       = false
    , inRegex         = false
		, onlyComment     = false
		, newStr          = ''
    , curLine         = ''
    , keepLineFeed    = false // Keep a line feed after comment
		, stringOpenWith
	;

	for (i = 0; i < str.length; ++i) {
		curChar  = str.charAt(i);
		nextChar = str.charAt(i + 1);
    curLine  = curLine + '' + curChar;

		// In string switcher
		if (
			   !inRegex
			&& !inComment
			&& (curChar === '"' || curChar === "'")
			&& (
				    (str.charAt(i - 1) !== '\\')
				 || (str.charAt(i - 2) + str.charAt(i - 1) == '\\\\')
				)
			) {
			if(inString && (curChar === stringOpenWith)) {
				inString       = false;
				stringOpenWith = null;
			} else if(!inString) {
				inString       = true;
				stringOpenWith = curChar;
			}
		}

		// In regex switcher
		if((!inComment && !inString) && (curChar === '/')) {
			if(inRegex
				// Not escaped ... /myregexp\/...../
				&& (str.charAt(i - 1) !== '\\')
				// Or escape char, previously escaped /myregexp\\/
				|| ((str.charAt(i - 1) === '\\') && (str.charAt(i - 2) === '\\'))) {
				inRegex = false;
			} else {
				if(~['=',',','('].indexOf(lastNoSpaceChar)) {
					inRegex = true;
				}
			}
		}

		if(!~['', ' '].indexOf(curChar)) {
			lastNoSpaceChar = curChar;
		}

		// we are not inside of a string or a regex
		if (!inString && !inRegex) {
      // Reset current line:
      if(curChar == '\n') {
        curLine  = '';
        if(inComment === 2) {
          keepLineFeed = true;
        }
      }

			// singleline comment start
			if (!inComment && curChar + nextChar === '/'+'/') {
				++i;
				inComment        = 1;
        keepLineFeed = (curLine.slice(0,-1).trim() != '');
			// singleline comment end
			} else if (inComment === 1 && curChar === '\n') {
				inComment = false;
        curChar   = keepLineFeed ? '\n' : '';
			// multiline comment start
			} else if (!inComment && curChar + nextChar === '/'+'*') {
				++i;
				inComment = 2;
				curChar = '';
			// multiline comment end
			} else if (inComment === 2 && curChar + nextChar === '*'+'/') {
				++i;
				inComment = false;
        curChar   = keepLineFeed ? '\n' : '';
			}
			if (inComment === 2 && curChar  === '\n') {
				curChar = '';
			} else if (inComment) {
				curChar = '';
			}
		}


		newStr += curChar;
	}

	return newStr;
}
function decojs(str) {
	var i
		, curChar, nextChar, lastNoSpaceChar
		, inString        = false
		, inComment       = false
    , inRegex         = false
		, onlyComment     = false
		, newStr          = ''
    , curLine         = ''
    , keepLineFeed    = false // Keep a line feed after comment
		, stringOpenWith
	;

	for (i = 0; i < str.length; ++i) {
		curChar  = str.charAt(i);
		nextChar = str.charAt(i + 1);
    curLine  = curLine + '' + curChar;

		// In string switcher
		if (
			   !inRegex
			&& !inComment
			&& (curChar === '"' || curChar === "'")
			&& (
				    (str.charAt(i - 1) !== '\\')
				 || (str.charAt(i - 2) + str.charAt(i - 1) == '\\\\')
				)
			) {
			if(inString && (curChar === stringOpenWith)) {
				inString       = false;
				stringOpenWith = null;
			} else if(!inString) {
				inString       = true;
				stringOpenWith = curChar;
			}
		}

		// In regex switcher
		if((!inComment && !inString) && (curChar === '/')) {
			if(inRegex
				// Not escaped ... /myregexp\/...../
				&& (str.charAt(i - 1) !== '\\')
				// Or escape char, previously escaped /myregexp\\/
				|| ((str.charAt(i - 1) === '\\') && (str.charAt(i - 2) === '\\'))) {
				inRegex = false;
			} else {
				if(~['=',',','('].indexOf(lastNoSpaceChar)) {
					inRegex = true;
				}
			}
		}

		if(!~['', ' '].indexOf(curChar)) {
			lastNoSpaceChar = curChar;
		}

		// we are not inside of a string or a regex
		if (!inString && !inRegex) {
      // Reset current line:
      if(curChar == '\n') {
        curLine  = '';
        if(inComment === 2) {
          keepLineFeed = true;
        }
      }

			// singleline comment start
			if (!inComment && curChar + nextChar === '/'+'/') {
				++i;
				inComment        = 1;
        keepLineFeed = (curLine.slice(0,-1).trim() != '');
			// singleline comment end
			} else if (inComment === 1 && curChar === '\n') {
				inComment = false;
        curChar   = keepLineFeed ? '\n' : '';
			// multiline comment start
			} else if (!inComment && curChar + nextChar === '/'+'*') {
				++i;
				inComment = 2;
				curChar = '';
			// multiline comment end
			} else if (inComment === 2 && curChar + nextChar === '*'+'/') {
				++i;
				inComment = false;
        curChar   = keepLineFeed ? '\n' : '';
			}
			if (inComment === 2 && curChar  === '\n') {
				curChar = '';
			} else if (inComment) {
				curChar = '';
			}
		}


		newStr += curChar;
	}

	return newStr;
}
function decojs ( str ) { var i , curChar , nextChar , lastNoSpaceChar , inString = false , inComment = false , inRegex = false , onlyComment = false , newStr = '' , curLine = '' , keepLineFeed = false // Keep a line feed after comment , stringOpenWith ; for ( i = 0 ; i < str . length ; ++ i ) { curChar = str . charAt ( i ) ; nextChar = str . charAt ( i + 1 ) ; curLine = curLine + '' + curChar ; // In string switcher if ( ! inRegex && ! inComment && ( curChar === '"' || curChar === "'" ) && ( ( str . charAt ( i - 1 ) !== '\\' ) || ( str . charAt ( i - 2 ) + str . charAt ( i - 1 ) == '\\\\' ) ) ) { if ( inString && ( curChar === stringOpenWith ) ) { inString = false ; stringOpenWith = null ; } else if ( ! inString ) { inString = true ; stringOpenWith = curChar ; } } // In regex switcher if ( ( ! inComment && ! inString ) && ( curChar === '/' ) ) { if ( inRegex // Not escaped ... /myregexp\/...../ && ( str . charAt ( i - 1 ) !== '\\' ) // Or escape char, previously escaped /myregexp\\/ || ( ( str . charAt ( i - 1 ) === '\\' ) && ( str . charAt ( i - 2 ) === '\\' ) ) ) { inRegex = false ; } else { if ( ~ [ '=' , ',' , '(' ] . indexOf ( lastNoSpaceChar ) ) { inRegex = true ; } } } if ( ! ~ [ '' , ' ' ] . indexOf ( curChar ) ) { lastNoSpaceChar = curChar ; } // we are not inside of a string or a regex if ( ! inString && ! inRegex ) { // Reset current line: if ( curChar == '\n' ) { curLine = '' ; if ( inComment === 2 ) { keepLineFeed = true ; } } // singleline comment start if ( ! inComment && curChar + nextChar === '/' + '/' ) { ++ i ; inComment = 1 ; keepLineFeed = ( curLine . slice ( 0 , - 1 ) . trim ( ) != '' ) ; // singleline comment end } else if ( inComment === 1 && curChar === '\n' ) { inComment = false ; curChar = keepLineFeed ? '\n' : '' ; // multiline comment start } else if ( ! inComment && curChar + nextChar === '/' + '*' ) { ++ i ; inComment = 2 ; curChar = '' ; // multiline comment end } else if ( inComment === 2 && curChar + nextChar === '*' + '/' ) { ++ i ; inComment = false ; curChar = keepLineFeed ? '\n' : '' ; } if ( inComment === 2 && curChar === '\n' ) { curChar = '' ; } else if ( inComment ) { curChar = '' ; } } newStr += curChar ; } return newStr ; }
Remove comment in a source code

decojs() is remove javascript style single line
and multiline comments (// and /*)

@param  {string} str The source to de-comment
@return {string}     Result string without comment
Remove comment in a source code