如何删除 javascript 中的评论?

How to strip comments in javascript?

我有多个文件以如下注释开头:

/*
 * @title Force email verification
 * @overview Only allow access to users with verified emails.
 * @gallery true
 * @category access control
 *
 * This rule will only allow access users that have verified their emails.
 *
 * > Note: It might be a better UX to make this verification from your application.
 *
 * If you are using [Lock](https://auth0.com/docs/lock), the default behavior is to log in a user immediately after they have signed up.
 * To prevent this from immediately displaying an error to the user, you can pass the following option to `lock.show()` or similar: `loginAfterSignup: false`.
 *
 * If you are using [auth0.js](https://auth0.com/docs/libraries/auth0js), the equivalent option is `auto_login: false`.
 *
 */
//jshint -W025
function (user, context, callback) {
  if (!user.email_verified) {
    return callback(new UnauthorizedError('Please verify your email before logging in.'));
  } else {
    return callback(null, user, context);
  }
}

所有文件都包含两种类型的注释,即 /**/// 现在我正在阅读我的 javascript 代码中的这个文件,我想删除注释并在变量例如

function (user, context, callback) {
  if (!user.email_verified) {
    return callback(new UnauthorizedError('Please verify your email before logging in.'));
  } else {
    return callback(null, user, context);
  }
}

我尝试过使用 strip-comments 和 parse-comments npm,但 none 这些工作。这是代码:

const fs = require('fs');
const path = require('path');
const strip = require('strip-comments');
module.exports = function (ruleFileName, globals, stubs) {
    globals = globals || {};
    stubs = stubs || {};
    const fileName = path.join(__dirname, '../src/rules', ruleFileName + '.js');
    const data = fs.readFileSync(fileName, 'utf8');
    const code = strip(data);
    console.log(code);
    return compile(code, globals, stubs);
}

我尝试使用解析注释:

const parsed = parseComments(data)[0];
const code = data.split('\n').slice(parsed.comment.end).join('\n').trim();

我认为条形注释不起作用,因为它采用字符串作为参数,但 fs.readFileSync 不采用 return 字符串。我也试过 data.toString(),但也没用。那么我怎样才能从内容中删除评论呢?还有其他解决办法吗?

尝试使用正则表达式替换 /\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm

   var Text = `/*
     * @title Force email verification
     * @overview Only allow access to users with verified emails.
     * @gallery true
     * @category access control
     *
     * This rule will only allow access users that have verified their emails.
     *
     * > Note: It might be a better UX to make this verification from your application.
     *
     * If you are using [Lock](https://auth0.com/docs/lock), the default behavior is to log in a user immediately after they have signed up.
     * To prevent this from immediately displaying an error to the user, you can pass the following option to "lock.show()" or similar: "loginAfterSignup: false".
     *
     * If you are using [auth0.js](https://auth0.com/docs/libraries/auth0js), the equivalent option is "auto_login: false".
     *
     */
    //jshint -W025
    function (user, context, callback) {
      if (!user.email_verified) {
        return callback(new UnauthorizedError('Please verify your email before logging in.'));
      } else {
        return callback(null, user, context);
      }
    }`

     console.log(Text.replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm,''))

像这样 https://codepen.io/anon/pen/eQKrWP