【问题标题】:finding regular expression literals in a string of javascript code在 javascript 代码字符串中查找正则表达式文字
【发布时间】:2011-12-17 16:57:12
【问题描述】:

我正在使用 javascript 对 javascript 代码进行一种粗略的解析。我将省略为什么我需要这样做的细节,但我只想说我不想想要集成大量的库代码,因为它是对我的目的来说是不必要的,重要的是我保持这个非常轻量级和相对简单。所以请不要建议我使用 JsLint 或类似的东西。如果答案比您可以粘贴到答案中的代码多,那可能比我想要的要多。

我的代码目前能够很好地检测带引号的部分和 cmets,然后匹配大括号、方括号和括号(当然,请确保不要被引号和 cmets 或引号内的转义符混淆)。这就是我需要它做的所有事情,而且它做得很好......除了一个例外:

它可能会被正则表达式文字混淆。因此,我希望在检测 javascript 字符串中的正则表达式文字方面得到一些帮助,以便我可以适当地处理它们。

类似这样的:

function getRegExpLiterals (stringOfJavascriptCode) {
  var output = [];
  // todo!
  return output;
}

var jsString =  "var regexp1 = /abcd/g, regexp1 = /efg/;"
console.log (getRegExpLiterals (jsString));

// should print:
// [{startIndex: 13, length: 7}, {startIndex: 32, length: 5}]

【问题讨论】:

  • 任何正则表达式文字?如果您只想要 // 中的内容,这很容易做到。
  • 我需要确定它是一个正则表达式文字,所以仅仅寻找斜杠是行不通的。

标签: javascript regex parsing


【解决方案1】:

es5-lexer 是一个 JS 词法分析器,它使用非常准确的启发式方法来区分 JS 代码中的正则表达式和除法表达式,并且还提供了一个令牌级别的转换,您可以使用它来确保生成的程序将被解释为相同通过完整的 JS 解析器和词法分析器。

确定/ 是否启动正则表达式的位在guess_is_regexp.js 中,测试从scanner_test.js line 401 开始

var REGEXP_PRECEDER_TOKEN_RE = new RegExp(
  "^(?:"  // Match the whole tokens below
    + "break"
    + "|case"
    + "|continue"
    + "|delete"
    + "|do"
    + "|else"
    + "|finally"
    + "|in"
    + "|instanceof"
    + "|return"
    + "|throw"
    + "|try"
    + "|typeof"
    + "|void"
    // Binary operators which cannot be followed by a division operator.
    + "|[+]"  // Match + but not ++.  += is handled below.
    + "|-"    // Match - but not --.  -= is handled below.
    + "|[.]"    // Match . but not a number with a trailing decimal.
    + "|[/]"  // Match /, but not a regexp.  /= is handled below.
    + "|,"    // Second binary operand cannot start a division.
    + "|[*]"  // Ditto binary operand.
  + ")$"
  // Or match a token that ends with one of the characters below to match
  // a variety of punctuation tokens.
  // Some of the single char tokens could go above, but putting them below
  // allows closure-compiler's regex optimizer to do a better job.
  // The right column explains why the terminal character to the left can only
  // precede a regexp.
  + "|["
    + "!"  // !           prefix operator operand cannot start with a division
    + "%"  // %           second binary operand cannot start with a division
    + "&"  // &, &&       ditto binary operand
    + "("  // (           expression cannot start with a division
    + ":"  // :           property value, labelled statement, and operand of ?:
           //             cannot start with a division
    + ";"  // ;           statement & for condition cannot start with division
    + "<"  // <, <<, <<   ditto binary operand
    // !=, !==, %=, &&=, &=, *=, +=, -=, /=, <<=, <=, =, ==, ===, >=, >>=, >>>=,
    // ^=, |=, ||=
    // All are binary operands (assignment ops or comparisons) whose right
    // operand cannot start with a division operator
    + "="
    + ">"  // >, >>, >>>  ditto binary operand
    + "?"  // ?           expression in ?: cannot start with a division operator
    + "["  // [           first array value & key expression cannot start with
           //             a division
    + "^"  // ^           ditto binary operand
    + "{"  // {           statement in block and object property key cannot start
           //             with a division
    + "|"  // |, ||       ditto binary operand
    + "}"  // }           PROBLEMATIC: could be an object literal divided or
           //             a block.  More likely to be start of a statement after
           //             a block which cannot start with a /.
    + "~"  // ~           ditto binary operand
  + "]$"
  // The exclusion of ++ and -- from the above is also problematic.
  // Both are prefix and postfix operators.
  // Given that there is rarely a good reason to increment a regular expression
  // and good reason to have a post-increment operator as the left operand of
  // a division (x++ / y) this pattern treats ++ and -- as division preceders.
  );

【讨论】:

  • 谢谢 Mike,我将来可能会使用完整的词法分析器,这是一项令人印象深刻的工作(就像你写的美化词一样,我已经广泛使用了)跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-02
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 2021-09-15
  • 2019-11-03
相关资源
最近更新 更多