The test function _ regular expression of the regular match

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.