Javascript (advanced tutorial) 1. Regular Expressions, cookie management, userData _ basic knowledge

Source: Internet
Author: User
Next, let's start our advanced journey and share more practical skills. First of all, what is a regular expression? In fact, the introduction of concepts often does not help us understand what it is, so let me briefly describe the regular expression, which is actually a string that records string rules. After reading this part, we can understand what it is.

Basic Syntax: the regular expression is the "/expression/" + symbol indicating the search range. For example, the keyword "function" is/function/gi, where g indicates global, global search, I indicates ignor, and case-insensitive.

In js, we use the RegExp class.

There are many symbols in this class to indicate different indexing methods. First, let's put a column in this table:
Metacharacters Description Quantifiers Description Negative characters Description
. Match any character except the newline character (\ n) * Number of occurrences: [0, + ∞) \ W Letters, numbers, underscores (_), other than Chinese Characters
^ Start of matching string + Number of occurrences: [1, +∞) \ S Characters other than white space characters
$ End of matching string ? Number of occurrences: [0, 1] \ D Characters other than numbers
\ B Match word boundary {N} Number of occurrences: n \ B Match non-word boundary
\ D Matching number {N ,} Number of occurrences: [n, +∞) [^] Any character other than the character string after the ^ character in the character class
\ S Match any blank space character {N, m} Number of occurrences: [n, m] [-] Match the characters/numbers in the string from-prefix to-suffix
\ W Match letters, numbers, underscores or Chinese Characters

In addition to the above symbols, there are three concepts: group, reverse reference, and candidate.
Grouping: Enclose strings with (), so that strings can be combined according to certain rules. At the same time, parentheses can also be nested. For example, the regular expression is used to express the Date Format: var dateReg =/^ (\ d })(-) (\ d {1, 2} (-) (\ d {1, 2}) $). Of course, this method also has some vulnerabilities. Here, it only indicates the format problem.
In addition to square brackets, for example, if you only want to match one of the letters x y z d w, write [xyzdw]. If the matching is continuous, for example, a 0-4 number can be [0-4], but it only represents one character.
If you want to write multiple, such as matching the ac or bd, use the "|" symbol to write (ac | bd ).
For example, if we want to match a string that only contains abc, we can write: abdReg =/^ [abc] + $/; below is a complete example.

The Code is as follows:




Regular express

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.