How do js regularities delete single-line comments?

this is what I originally wrote:

var s = "var a = "test"; // 1234678"
s.replace(/\/\/[\s\S]*?\n?$/,"");

later found that there is such a code in the code:

var a = "://"+host;// 123456
var s = "  //  tset "; // test
var a = "http://www.a.com//b/c";

so it"s so complicated. I don"t know how to write this rule. =

The regularity of

JS does not support (? 'xx') and (? -'xx') doesn't feel good to indicate state, so parse the code yourself


I have written this kind of code. At present, I can solve the matching of the code that I have thought of, and there must be a trap

.
str.replace(/[\x20\S]*\/\/[\x20\S]*?\n?$/gm, function(v) {
        if (/^\x20*\/\/|^\x20*\/\//.test(v)) { return '' }
        if (/[,;]\x20*\/\//.test(v)) { return v.replace(/([,;])\x20*\/\/[\x20\S]*?\n?$/m, '$1') }
        return v
    })

later found this code:

{
    a:"aa",
    b:"bb"//
}

wtf


it is impossible to write regularities that are applicable to all cases. Regular expressions cannot solve problems such as matching parentheses and quotation marks. If you want to solve your problem, you have to apply all situations, including cases where multiple strings are deliberately written in one line. It is impossible without parsing, but you can still use regularities to solve some problems, and some special cases can be solved. You can try asserting something


has made a lot of attempts. Take a look at this.

removeComment(`' : //'+host;// 123456`); // ' : //'+host;

function removeComment(str) {
  const reg = /("([^\\\"]*(\\.)?)*")|('([^\\\']*(\\.)?)*')|(\/{2,}.*?(\r|\n|$))|(\/\*(\n|.)*?\*\/)/g;
  return str.replace(reg, function(s) { 
    return /^\/{2,}/.test(s) || /^\/\*/.test(s) ? "" : s; 
  });
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35696-4ebf7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35696-4ebf7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?