var fntest =/xyz/.test (function () {xyz;})? /b_superb/:/.*/; For the use of the regular expression matching function test, I was puzzled:
/xyz/.test (function () {xyz;}) I checked the description of the test function in Mozilla Developer Center:
Executes the search for a match between a regular expression and a specified string. Returns true or False.
Whether there is a substring in the string that matches the regular expression. Returns TRUE or FALSE.
Its syntax format:
Regexp.test ([str]) Note that the argument here is a string and there is no reference to the argument that the parameter of the test function can be a function.
I tried to modify this strange code and get some running results:
Copy Code code as follows:
/xyz/.test ("xyz"); True
/xyz/.test (function () {"XYZ";}); False
/xyz/.test (function () {return "XYZ";}); True
/xyz/.test (function () {return xyz;}); True
/xyz/.test (function () {return axyz;}); True
/xyz/.test (function () {return "axyz";}); True
/xyz/.test (function () {return "Xayz";}); False what's going on?
Gorgeous split Line
=======================================
I finally know, but originally in the test inside the type conversion, the following code:
Copy Code code as follows:
/xyz/.test (function () {xyz;});
Equivalent to:
Copy Code code as follows:
/xyz/.test (function () {xyz;}). ToString ());
Also equivalent to:
Copy Code code as follows:
/xyz/.test ("function () {xyz;}");
So why does the following code return false?
Copy Code code as follows:
/xyz/.test (function () {"XYZ";});
We'll know by executing this line of code:
Copy Code code as follows:
The execution result of this line of code is: "function () {}"
(function () {"XYZ";}). ToString ();
So we can write more bizarre code to confuse everyone.
Copy Code code as follows:
/function/.test (function () {}); True
b denotes the text boundary (for English)
/b_superb/.test (function () {this._super ();}); True