Different result from JavaScript RegExp in “rhino1.7.6” vs “V8”

让人想犯罪 __ 提交于 2019-12-07 22:58:03

问题


currently, I found the same JavaScript RegExp might generate different result in different JavaScript Engines, here is an example: In Chrome V8 JS engine,

/\x3c/.test("\x3c")  --> returns true

/\x3c/.test(function() {  return "\x3c"  }) -->returns ***false***

In rhino1.7.6,I typed the command like this:

>java -jar js.jar

  Rhino 1.7.6 2015 04 15

js> /\x3c/.test(function() {  return "\x3c"  })

  true

js>

And I tested these two:

/\x3c/.test("\x3c")  --> returns true

/\x3c/.test(function() {  return "\x3c"  }) -->returns ***true***

I am wondering why these two engines generate different result. I believe they must be compliant with some standards.

Please correct me if I am wrong. And moreover, if it is a special occasion, would you please kindly tell me if there are some configurations I can tell JS engine on these special occasions.


回答1:


Given the value function() { return "\x3c" }

  • Chrome converts it to the string "function() { return "\x3c" }", that is, a literal backslash followed by "x3c".

  • Rhino converts it to the string "function () { return "<"; }", that is, it reformats the function definition.

Both are actually correct behaviour. The ECMA-262 (a.k.a. Javascript) standard says that String(func) should return "an implementation-dependent String source code representation of func."



来源:https://stackoverflow.com/questions/31714382/different-result-from-javascript-regexp-in-rhino1-7-6-vs-v8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!