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 section. Code And some running results are obtained:
Copy code The 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