JavaScript Regular expression Repeat escape character class detailed

Source: Internet
Author: User
Tags character classes character set regular expression repetition

Repeating character for regular expression:
character meaning
{N,m} matches the previous item at least n times, but not more than m times
{N,} matches the previous n times, or more times
{n} matches the previous item exactly n times
? Matches the previous item 0 or 1 times, which means the previous item is optional. Equivalent to {0,1}
+ Match previous 1 or more times, equal to {1,}
* Match previous 0 or more times, equal to {0,}
Example:

The code is as follows Copy Code

All 10 digits, to four digits, including: 0000
var pattern =/d{2,4}/;

The following regular content matching AB,ABC,ABCCCC
var pattern =/abc*/;

The following regular content matching AB,ABBB
var pattern =/ab+/;

Note: Be careful when using duplicate characters * and?, because these characters may match 0 matches of the preceding characters, so they allow nothing to match. For example, the regular expression/a*/actually matches the BBBB, because the character contains 0 letters a.
Non-greedy repetition:
Some of the repetitions I've just described will match as many characters as possible and allow the next regular expression to continue to match. Because the above repetition is "greedy". In JavaScript regular expressions, you can repeat in a non greedy way, which is a feature of PERL5, but is implemented by JavaScript.
Define a method that is not greedy: Simply add a question mark after the repeating character, for example:?? ,? +,? * even {1,5}? This is also possible.
Examples are as follows:
Normally,/a+/matches one or more character a, and when applied to the character AAA, it matches three letters. But/a+?/only matches one or more necessary words.
Mother A
For example,/a*b/matches one or more letters a followed by a letter B, which is matched B,aaab,ab also matches AAB, but if it is changed to a non greedy repetition, that is/a*b?/it only matches B, and does not match AB, with Aaab.


character Classes--aggregating with enumeration definitions:
As already explained in the example given to you, the character class defines a clustered enumeration. A character class matches any character it contains. You can see from the example I gave you, no? What is the character class for/> BR?
JavaScript's regular expressions support Boolean operations, and if we want to match the letter "a", "B" or "C", we can define it in boolean form by using the direct amount of the regular expression mentioned earlier, as shown in the following example:
For the purpose of stating that regular expressions support Boolean operations

The code is as follows Copy Code
var pattern =/a|b|c/;

Character classes are used because when you want to match a lot of members, it's obviously much simpler to use a character class than Boolean operations.
Character class--support scope definition:
The character class, in addition to the enumeration definition aggregation, also supports the scope definition, which has the syntax structure:/[begin_data-end_data]/, and the following example:
Define regular expression match 0-9

The code is as follows Copy Code
var pattern =/[0-9]/;

Define regular expression matches number 0-9 and letter A-Z
var pattern =/[0-9a-z]/Character Class--supports Boolean non-operation:
Uh, what do you mean, Bulfei? Is the boolean inside of the reverse operation, which is not well. You say is, then I want negative, you say greater than I say less than equal. That's the truth. Character classes also support Boolean operations, with the addition of the symbol "^" in the front of the character set in parentheses.
Match any one character except the letter A,b,c

The code is as follows Copy Code
var pattern =/[^abc]/;

JavaScript character class abbreviation:
Character class abbreviation This is my name, hehe. Because of the above points, we can also see that the character class is quite easy to use. For this reason, the regular expression syntax for JavaScript contains special characters and escape sequences to represent these commonly used classes (most programming languages now support such operations), making it easy for you to use them. Below I have listed in the Book of character abbreviations for everyone to list out, easy to read, uh. However, it should be noted that some character class escape sequences match only ASCII characters and are not extended to handle Unicode characters and can be displayed to define their own Unicode character classes.
Character matching
[...] Any character that is within the parentheses
[^...] Any character in the not-abundant bracket
. Any character except line breaks and other Unicode line terminators
W any ASCII word character, equivalent to [a-za-z0-9_], that is, numbers, letters, underscores
W any ASCII non-word character, equivalent to [^a-za-z0-9_]
s any Unicode blank character
s any non-Unicode whitespace, note that W and s are different
D any ASCII number, equivalent to [0-9]
D any character other than the ASCII number, equivalent to [^0-9]
[b] Backspace Direct quantity (special case)

Escape rule for


character:
character match
alphanumeric character self
o null character (u0000)
T tab (u0009)
N line Break (u000a)
V Vertical tab (U000B)
F page Break (u000c)
R return (u000d)
Xnn The Latin character specified by the hexadecimal number NN, for example, x0a equivalent to n
uxxxx Unicode characters specified by the hexadecimal number xxxx , for example, u0009 is equivalent to the T
CX control character ^x. For example, CJ is equivalent to the newline character N
punctuation with special meaning:
Unlike a regular string, some symbols in a regular expression have special meanings and must be escaped when used as normal characters, including:
^ $. * +? =!: |/() [] {}
These special punctuation marks are explained in subsequent logs.

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.