如何在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 但这些都不起作用。这是代码:

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-comments 我尝试过:

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

我认为条带注释不起作用,因为它将字符串作为参数,但 fs.readFileSync 不返回字符串。我也尝试过,data.toString()但这也不起作用。那么如何从内容中删除评论呢?还有其他解决方案吗?

蔡火星

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Rails博客应用中删除评论?

来自分类Dev

如何在chunk-vendors.js中删除评论

来自分类Dev

如何从上传的javascript文件中删除所有评论?

来自分类Dev

如何使用JavaScript在评论中添加删除按钮

来自分类Dev

使用JavaScript中的替换删除评论

来自分类Dev

Gulp-从javascript文件中删除评论

来自分类Dev

如何在浏览器视图页面源中隐藏或删除评论?

来自分类Dev

如果多行已经包含评论,如何在 sublime 中评论?

来自分类Dev

如何在 Laravel 中显示评论和回复评论

来自分类Dev

如何在Rails中显示用户评论

来自分类Dev

如何在PHP中显示评论通知

来自分类Dev

如何在Django帖子中添加评论

来自分类Dev

如何在Fastlane文件中写评论

来自分类Dev

如何在Java中添加评论框?

来自分类Dev

如何在django中显示评论数

来自分类Dev

如何在 Facebook 中实现编辑评论?

来自分类Dev

如何在特定帖子中添加评论?

来自分类Dev

如何在JavaScript中删除子级

来自分类Dev

如何在JavaScript中删除子级

来自分类Dev

从文件中删除评论

来自分类Dev

从文件中删除评论

来自分类Dev

如何在通过评论 API 添加评论时在跑道评论中应用富文本格式

来自分类Dev

如何在不丑陋的情况下删除评论?

来自分类Dev

如何在嵌入式Yesod JavaScript模板中添加评论

来自分类Dev

grunt-contrib-cssmin-如何从缩小的CSS中删除评论

来自分类Dev

如何从文本文件中删除评论

来自分类Dev

如何从模块前台输出中删除评论块

来自分类Dev

在PaperTrail中,如何在每个版本中记录评论?

来自分类Dev

在PaperTrail中,如何在每个版本中记录评论?