The "pit" of the brackets in the JavaScript regular expression

Source: Internet
Author: User
Tags character classes

The use of regular in JavaScript requires attention to a pit inside the brackets, which is the meta-character problem within the brackets. I stepped on the pit, online search and a lot of people stepped on the pit, so probably say a bit.

The brackets are called character groups (Character Class) in the regular, some are translated into character classes , and others are translated into character sets . I think the character group is better, after all, class in the computer belongs to the object-oriented "class." As the name implies, a character group is a set of characters that represent multiple characters that may appear in one place. Note that this emphasis only matches one location. (This paragraph was excerpted from-http://www.cnblogs.com/snandy/p/3662423.html)

Use a place to take any character, and then use the [. \n]* result Test to return true, but not in the context of the specific ... Gee, I can't understand.

A decisive test of var TESTP =/xx>[.\n]*/testp.test ("XX>ASDFDFASD"); Decisive or true, put "xx>" Back is also true, but both sides are put on is false. What is this for??????

Then re-put the var TESTP =/(xx>[.\n]*)/This, change the parentheses to catch a matching string . Test finished,regexp.$1 to find out is empty .... is empty .... Why????

When the string parameter of test becomes ("..."), this dot appears, so, the inside of this bracket is only the point, not the other, not the meta-character except \ n the meaning of single characters. So I searched the internet for a moment:

"A lot of metacharacters have become ordinary characters within a character group." it becomes a normal character instead of a meta character, and there are other like "?", "+", "*" which once placed in brackets also become ordinary characters.

So the pit still has to be careful ....

By the right, any character can be replaced with [\s\s].

The "pit" of the brackets in the JavaScript regular expression

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.