Check [pitfalls] in the regular expressions in javascript and brackets in the regular expressions.

Source: Internet
Author: User

Check [pitfalls] in the regular expressions in javascript and brackets in the regular expressions.

When using regular expressions in javascript, you need to pay attention to a pitfall in brackets, that is, the metacharacters in brackets. I stepped on the trap myself. I searched the internet and many others stepped on the trap, so I 'd like to explain it.

Brackets are called in Regular ExpressionsCharacter Group(Character class), some books are translatedCharacter class, And translateCharacter Set. I think character groups are better. After all, the class in the computer represents the "class" in the object-oriented ". As the name suggests, a character group is a group of characters, which indicate multiple characters that may appear in a single position. Note that only one location is matched here. (This section is taken from-http://www.cnblogs.com/snandy/p/3662423.html)

 

A place where any character is used, so the result of [. \ n] * test is used to return true, but it is not correct in the specific environment ...... I can't understand it anymore.

Decisively test var testp =/xx> [. \ n] */testp. test ("xx> asdfdfasd"); whether it is decisive or true. It is true to put "xx>" after it, but false to both sides. Why ??????

 

So we re-run var testp =/(Xx> [. \ n] *)/Modify it.ParenthesesYou canThe matched string is captured.. Test is complete,RegExp. $1 click it to find it is null ..... Is null ....Why ????

When the string parameter of test is changed ("........... "), this string appears, so the dot in the brackets only represents the dot, not anything else, not the meaning of a single character except \ n. So I searched the internet:

"Many characters are converted into common characters in the character group"It becomes a common character, not a metacharacter, and there are other images like "? "," + "," * ", Once put in brackets, it becomes a common character.

So be careful about this trap ....

 

By the way,Any character can be replaced by [\ s \ S.

The [pitfalls] in the above section of the javascript regular expression are all the content shared by the editor. I hope to give you a reference and support for the customer's home.

Related Article

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.