Var fnTest =/xyz/. test (function () {xyz ;})? /B _superb/:/. */; I am confused about the usage of the regular expression matching function test:
/Xyz/. test (function () {xyz;}) I checked the test function description in Mozilla developer center:
Executes the search for a match between a regular expression and a specified string. Returns true or false.
Whether the string contains a substring that matches the regular expression. Returns true or false.
Syntax format:
Regexp. test ([str]) Note that the parameter here is a string, and it is not mentioned that the parameter of the test function can be a function.
I tried to modify this strange code and got some running results:
Copy codeThe Code is 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" ;}); // What is false?
Gorgeous split line
========================================================
I finally learned, but the type conversion was originally performed inside test. The following code:
Copy codeThe Code is as follows:
/Xyz/. test (function () {xyz ;});
It is equivalent:
Copy codeThe Code is as follows:
/Xyz/. test (function () {xyz;}). toString ());
It is equivalent:
Copy codeThe Code is as follows:
/Xyz/. test ("function () {xyz ;}");
So why does the following code return false?
Copy codeThe Code is as follows:
/Xyz/. test (function () {"xyz ";});
After executing this line of code, we will know:
Copy codeThe Code is as follows:
// The execution result of this line of code is: "function (){}"
(Function () {"xyz" ;}). toString ();
So we can write more weird code to confuse everyone.
Copy codeThe Code is as follows:
/Function/. test (function () {}); // true
// B indicates the text boundary (for English)
/B _superb/. test (function () {this. _ super () ;}); // true