来自“rhino1.7.6”中的JavaScript RegExp的不同结果。 vs“V8” [英] Different result from JavaScript RegExp in "rhino1.7.6" vs "V8"
问题描述
目前,我发现相同的JavaScript RegExp可能会在不同的JavaScript引擎中生成不同的结果,这是一个示例:
在Chrome V8 JS引擎中,
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***
在rhino1.7.6中,我输入了如下命令:
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>
我测试了这两个:
/\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.
如果我错了,请纠正我。而且,如果这是一个特殊的场合,请您告诉我是否有一些配置我可以在这些特殊场合告诉JS引擎。
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.
推荐答案
给定值 function(){return\ x3c}
-
Chrome 将其转换为字符串
function(){return\ x3c}
,即,字面反斜杠后跟x3c。
Chrome converts it to the string
"function() { return "\x3c" }"
, that is, a literal backslash followed by "x3c".
Rhino 将其转换为字符串函数( ){return<;}
,即重新格式化函数定义。
Rhino converts it to the string "function () { return "<"; }"
, that is, it reformats the function definition.
两者都是正确的行为。 ECMA-262(又名Javascript)标准说 String(func)
应该返回 func 的依赖于实现的字符串源代码表示。
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."
这篇关于来自“rhino1.7.6”中的JavaScript RegExp的不同结果。 vs“V8”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!