Written in JS and used web pages as the carrier. You are welcome to test it. Because I have never learned algorithms and I don't know whether to write them as I think. Use them together. Let's talk about your understanding. Single line comment ://... or /*... */multi-line comment :/*...... */if these characters are in the string, they are not comments; if the string is in the comment, they are not strings; and the special Syntax of JS regular expressions is messy; there are also escape characters/(backslash). Therefore, it is quite troublesome. I 've written a lot of comments, and I'm afraid I forget it. It's better to use it.
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en"> <HTML> Function deletecomment () {var t1_content = document. all. t1.value; // output variable VAR result = ""; // global flag, used to handle multiline comments var flag = false; // split by line, B is the string array var B = t1_content.split ("/R/N"); // For per row // for each row for (VAR I = 0; I <B. length; I ++) {var perrow = B [I]; // string of the current row // if it is a multiline comment if (flag = true) {// determine whether there is a multiline comment Terminator if (perrow. indexof ("*/")! =-1) {// exist "*/" // Delete comment // get */The character following perrow = perrow. substr (perrow. indexof ("*/") + 2); // reset the multi-line comment mark and use flag = false next time; // The process continues, because the remaining strings in this row may have comments} else {// does not exist, it indicates that the current row is still part of the multiline comments, continue to check the next row // and do not accumulate this row into the output variable continue; // The process does not go down} // for each row, determine whether double quotation marks exist "or comment the start character/If (perrow. indexof ("/" ") =-1 & perrow. indexof ("/") =-1) {// neither "nor/exsit in perrow // do nothing // both do not exist and do nothing, check the next line} else if (PE Rrow. indexof ("/"")! =-1 & perrow. indexof ("/") =-1) {// only "exist in perrow // do nothing // There are only double quotation marks and nothing needs to be done, check the next row // the above two judgments can be simplified into one} else {// if there is "or/, or both exist // in each row, determine each character // For per character for (var j = 0; j <perrow. length; j ++) {// retrieve each character var tempchar = perrow. substr (J, 1); // per char // if it is ", the matching if (tempchar ="/"") of the next "must be performed "/"") {// calculate the next character position (INDEX) var tempcol = J + 1; if (tempcol = perrow. length) {// if there is no next character, check the next line // I don't think this can be judged, because the code to be processed is compiled correctly, // This will not happen // end the current row for processing and accumulate the break; // next row} else {// If there is a next character, cyclically judge for (var k = tempcol; k <perrow. length; k ++) {// retrieves the next character var nextchar = perrow. substr (k, 1); // if it is ", you must determine whether the first character of it is an escape character/If (nextchar = "/"") {// retrieve the previous character var temp = perrow. substr (K-1, 1); // if it is an escape character if (temp = "//") {// continue to check next character continue ;} else {// match "... "// if it is not/, match the previous pair" J = K; // jump out of the matching loop of double quotation marks of the string. Check break for the next character; // next Col }}}} else if (tempchar = "/") {// if it is a comment start character, // calculate the location of the next character var tempcol = J + 1; if (tempcol = perrow. length) {// if there is no next character, the current row is processed and break is accumulated in the result; // next row} else {// if the next character is, you have to determine whether the character above is a transfer character. // The purpose of this operation is to avoid the regular expression if (perrow. substr (tempcol, 1) = "/") {// determine if it is the first character if (J! = 0) {// if it is not the first character, check whether the character before it is/If (perrow. substr (J-) = "//") {// Regexp // Like: // * ASDF * // If yes, it indicates that this is a regular expression, not a comment, // check the next character of this line for continue;} // if the current character is/, its next character is also /, its first character is not/, indicating that this is a single line comment // The string before this character is taken as the result after the comment is deleted, accumulate to output variable // Delete comment perrow = perrow. substr (0, J); // jumps out, ends processing of the current row, and accumulates break in the result; // next row} else if (perrow. substr (tempcol, 1) = "*") {// It is also used to avoid the regular expression if (J! = 0) {If (perrow. substr (J-1,1) = "//") {// Regexp // Like: // * ASDF * // continue ;}} // if the current character is/, its next character is *, and its first character is not /, it indicates that this is a/* Form annotation // set the multi-line annotation flag to true flag = true; // remove the remaining strings of this row var temp = perrow. substr (J + 2); // check whether there is */If (temp. indexof ("*/")! =-1) {// Delete comment // If yes, put /*... */delete perrow = perrow. substr (0, j) + temp. substr (temp. indexof ("*/") + 2); // reset the flag, indicating that the flag has been deleted. Flag = false; // After the comment is deleted, J has pointed to the next character, after this logic is processed, // executes the J ++ of the loop for (each row), so that a character is skipped. // J -- j -- is required first --; // important} else {// Delete comment // if the remaining strings in this row are not found */, it indicates that this is a multiline comment // Delete the remaining string (retrieve the previous string as the result) perrow = perrow. substr (0, J); // ends processing of the current row, and the result accumulates break; // next row} else {// The next character is neither/nor *, // then, this is not a comment start character. Check the next character before performing the next line, add the result to the output variable result + = perrow + "/R/N";} document. all. t2.value = Result ;}</SCRIPT>