在javascript代码字符串中查找正则表达式文字
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在javascript代码字符串中查找正则表达式文字,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3796字,纯文字阅读大概需要6分钟。
内容图文
我正在使用javascript对javascript代码进行粗略的解析.我会详细说明为什么我需要这样做,但足以说我不想整合大量的库代码,因为它对我的目的来说是不必要的,重要的是我保持这个非常轻量级而且相对简单.所以请不要建议我使用JsLint或类似的东西.如果答案的代码多于您可以粘贴到答案中的代码,那么它可能比我想要的更多.
我的代码目前能够很好地检测引用的部分和注释,然后匹配括号,括号和parens(当然,确保不要被引号和注释混淆,或者在引号内转义).这就是我需要它做的全部,并且它做得很好……但有一个例外:
它可能会被正则表达式文字混淆.所以我希望在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}]
解决方法:
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.
);
内容总结
以上是互联网集市为您收集整理的在javascript代码字符串中查找正则表达式文字全部内容,希望文章能够帮你解决在javascript代码字符串中查找正则表达式文字所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。