If you want to use a RegExp, you could use this one:(如果要使用RegExp,可以使用以下一种:)
/(/*[^*]**/)|(//[^*]*)/
This should strip both // ... \n
style comments and /* ... */
style comments.(这应该去除// ... \n
样式注释和/* ... */
样式注释。)
Full working code:(完整的工作代码:)
var stringWithoutComments = s.replace(/(/*[^*]**/)|(//[^*]*)/g, '');
console.log(stringWithoutComments);
Test with multiline strings:(用多行字符串测试:)
var s = `before
/* first line of comment
second line of comment */
after`;
var stringWithoutComments = s.replace(/(/*[^*]**/)|(//[^*]*)/g, '');
console.log(stringWithoutComments);
outputs:(输出:)
before
after
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…