问题
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