Nodejs无效正则表达式

时间:2014-07-22 20:46:51

标签: javascript regex node.js jsdoc jsdoc3

我运行以下命令:

node_modules/.bin/jsdoc -r --destination /home/user/public_html/jsdoc-meta/ node_modules/jsdoc/

这就是我得到的。我在正则表达式中找不到错误:

/home/user/node_modules/jsdoc/lib/jsdoc/name.js:77
    var regexp = new RegExp('^' + memberof.toString() + REGEXP_SCOPE_PUNC);
                 ^
SyntaxError: Invalid regular expression: /^harmonyTestFixture.ES6 Template Strings.`\u{000042}\u0042\x42\u0\102\A`[~,#,.]/: Unterminated character class
    at new RegExp (<anonymous>)
    at nameIsLongname (/home/user/node_modules/jsdoc/lib/jsdoc/name.js:77:18)
    at Object.exports.resolve (/home/user/node_modules/jsdoc/lib/jsdoc/name.js:114:21)
    at Doclet.postProcess (/home/user/node_modules/jsdoc/lib/jsdoc/doclet.js:171:20)
    at newSymbolDoclet (/home/user/node_modules/jsdoc/lib/jsdoc/src/handlers.js:190:23)
    at null.<anonymous> (/home/user/node_modules/jsdoc/lib/jsdoc/src/handlers.js:226:29)
    at EventEmitter.emit (events.js:98:17)
    at Visitor.visitNode (/home/user/node_modules/jsdoc/lib/jsdoc/src/visitor.js:283:16)
    at Visitor.visit (/home/user/node_modules/jsdoc/lib/jsdoc/src/visitor.js:177:27)
    at Walker.recurse (/home/user/node_modules/jsdoc/lib/jsdoc/src/walker.js:533:27)

1 个答案:

答案 0 :(得分:1)

节点(或者更确切地说是V8)尝试将其解析为UTF8转义序列:

/^harmonyTestFixture.ES6 Template Strings.`\u{000042}\u0042\x42\u0\102\A`[~,#,.]/
                                                                ^^^^^^

但是,由于\u0102之间的反斜杠缺少字符,因此错误消息(Unterminated character class)。根据您要实现的目标,您可以删除该反斜杠:

/^harmonyTestFixture.ES6 Template Strings.`\u{000042}\u0042\x42\u0102\A`[~,#,.]/
                                                                  ^

或逃避反斜杠:

/^harmonyTestFixture.ES6 Template Strings.`\u\{000042\}\u0042\x42\u0\\102\A`[~,#,.]/
                                                                    ^^