【问题标题】:How to strip comments in javascript?如何在javascript中去除注释?
【发布时间】:2019-04-27 18:39:00
【问题描述】:

我有多个以 cmets 开头的文件,例如:

/*
 * @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);
  }
}

所有文件都包含两种类型的 cmets,即 /**/// 现在我正在我的 javascript 代码中读取此文件,并希望删除 cmets 并获取变量中的实际代码,例如

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-cmets 和 parse-cmets npm,但这些都不起作用。代码如下:

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);
}

我尝试使用 parse-cmets:

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

我认为条形注释不起作用,因为它将字符串作为参数,但 fs.readFileSync 不返回字符串。我也试过data.toString()但这也没有用。那么如何从内容中去除 cmets 呢?有没有其他解决办法?

【问题讨论】:

  • 已经看到了,但它不起作用,因为 strip-cmets 将字符串作为参数
  • 如果您使用 Komodo Edit,您可以跨多个文件使用 RegEx。 cntrl+h.
  • "但是 fs.readFileSync 不返回字符串" - 实际上,it does return a string 因为你传递了一个编码选项。

标签: javascript node.js npm ecmascript-6 auth0


【解决方案1】:

尝试使用正则替换/\/\*[\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

【讨论】:

  • 谢谢,它可以工作,但我必须在最后添加 .trim() 才能使其正常工作。即 data.replace(/\/*[\s\S]*?*\/| ([^:]|^)\/\/.*$/gm, '').trim()
猜你喜欢
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
  • 2020-01-27
  • 2010-11-08
相关资源
最近更新 更多