如何在javascript中剥离注释?
我有多个以注释开头的文件,如:如何在javascript中剥离注释?,javascript,node.js,npm,ecmascript-6,auth0,Javascript,Node.js,Npm,Ecmascript 6,Auth0,我有多个以注释开头的文件,如: /* * @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. * * >
/*
* @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);
}
}
我曾尝试使用剥离注释和解析注释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);
}
对于解析注释,我尝试如下:
const parsed = parseComments(data)[0];
const code = data.split('\n').slice(parsed.comment.end).join('\n').trim();
我认为strip comment不起作用,因为它将string作为参数,但fs.readFileSync不返回string。我也尝试了
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,''))
像这样
查看此[link]已经看到了这一点,但它不起作用,因为strip comments将字符串作为参数。如果使用Komodo Edit,可以跨多个文件使用正则表达式
cntrl+h
“但是fs.readFileSync不返回字符串”-实际上,因为您传递了一个编码选项。谢谢,它可以工作,但我必须在最后添加.trim(),以使其工作。即data.replace(/\/*[\s\s]*?*\/\\/|([^:][124; ^:][124; ^^^)\/\/.$/gm