Regular expression is an object describing character mode. JavaScript Regexp objects and String objects define the use of regular expressions to perform powerful mode matching and text retrieval and replacement functions.
'**********************' // javascript // '******************* *****
In JavaScript, the regular expression is represented by a regexp object. Of course, a regexp () constructor can be used to create a regexp object, or a newly added special syntax in JavaScript 1.2 can create a regexp object. Just like String direct volume is defined as characters contained in quotation marks, and the regular expression direct amount is also defined as characters included in a pair of slash (/). So, JavaScript may contain the following code:
Var pattern = / s $ /;
This line code creates a new regexp object and assigles it to the variable parttern. This special regexp object matches all strings ending with the letters "S". Use regexp () to define an equivalent regular expression The code is as follows:
Var pattern = new regexp ("s $");
Whether it is using regular expressions or use constructor regexp (), creating a regexp object is easier. More difficult tasks is to describe characters in the characteristic expression syntax. JavaScript uses Perl language regular expression. A fairly complete subset of syntax.
The pattern specification of the regular expression is made up of a series of characters. Most characters (including all alphanumeric characters) are characterized by literally, which is said, regular expression / java / and all included The string "Java" string matches. Although other characters in the regular expressions are not matched according to the literal meaning, they all have special meaning. Regular expressions / S $ / contain two characters. First Special characters "S" are matched to itself. The second character "$" is a special character, which matches the end of the string. So regular expression / s $ / match is the letter " S "end strings.
Direct quantity character
We have discovered that all alphabet characters and numbers in the regular expression are all matched with themselves. JavaScript's regular expression also supports certain non-
Alphabetic characters. For example, the sequence "/ n" matches a direct quantity in the string. In the regular expression, many punctuation symbols have special meanings. The following is these characters and their meaning:
Direct quantity character of regular expression
Character matching __________________________________________________________ Quantity / * A * direct quantity / one direct amount /? One? Direct quantity / | 1 | Delin / (1 (direct quantity /) one) direct quantity / [1 [direct quantity /] one] direct quantity / {1 {direct quantity /} one} direct quantity / xxx The ASCII code character / XNN specified by the decimal number XXX is specified by the hexadecimal number nn. For example, / ci equivalent to / t, / cj equivalent to / N ___________________________________________________________________________________________________________________________________________________________________________________________________________________________________
If you want to use special punctuation symbols in regular expressions, you must add one "/" before they.
2. Character class
Put a separate direct conjunction into the brackets to make a character class. A character class matches any of the characters it contains, so regular expression / [ABC] / and letters "A", "B", Any of "c" matches. You can also define the negative character class, which matches all characters that are included in characters that are included in the brackets. To define the negative character tip, you should use a ^ symbol as The first character calculated from the parentheses from the left. The collection of regular expressions is / [A-ZA-Z0-9] /.
Since some character classes are very common, JavaScript's regular expression syntax contains some special characters and escape sequences to represent these common class. For example, / s matches space characters, tabs, and other blank characters, / s Matching is any character outside the blank character.
Regular table gray character class
Character matching _________________________________________________________________________________________________________________________________________________________ ^ ...] is not in any character among parentheses. In addition to any character other than the wrapper, equivalent to [^ / n] / w Single-character character, equivalent to [A-ZA-Z0-9] / W any non-single character, equivalent to any blank character, equivalent, equivalent [/ t / N / R / f / v] / S any non-blank character, equivalent to any number of [^ / t / n / r / f / v] / D, equivalent to [0-9] / D except the number Character, equivalent to [^ 0-9] [/ b] a straight quantity (special case) ____________________________________________________
With the above-mentioned regular representation, the two digits can be described in / / d / d /, describe the four digits into / / d / d / d / d /. But there is no way to use it. Describe the numbers with any number of bits or one
String. This string is composed of three characters and a digit following the letter after the letter. These complex mode uses the regular expression syntax specified that each element is repeated in the expression.
Specifying replication characters always appear behind the mode they act. So some special characters are specifically used to represent them. For example: number is the previous mode once
Or multiple times. The following table lists the copying syntax. First look at an example:
// D {2, 4} / / / Match 2 to 4 numbers.
// w {3} / d? / / Match three single characters and an arbitrary number.
// s Java / S / / / Match string "java" and there can be one or more spaces before and after the string.
/ [^ "] * / / / Match zero or more non-quotient characters.
Copy characters of regular expressions
Character meaning __________________________________________________________________ {n, m} before a match at least n times, but not more than m times {n,} matches the first n times one or more times before a {n} matches exactly n times? Before a match Item 0 times or 1, that is, the previous item is optional. Equivalent to {0, 1} , one item 1 or more, equivalent to {1,} * Match the previous 0 Second or multiple times. Equivalent to {0,} ______________________________________________________ 4. Select, Packet, and Quote
Regular expression syntax also includes specifying a selection, pair sub-expression grouping, and a special character for a previous child expression. Character | is used to separate the selected character. For example: / ab | cd | EF / match is character. String "ab", or
String "CD", or "EF". // D {3} | [AZ] {4} / match is either a three-digit, or four lowercase letters. In regular expressions Several effects. Its main role is to group separate items
Sub-expression in order to handle those items like *, or?, Such as: / java (script)? / Match is the string "java", thereafter can be "Script "Or no. /
(AB | CD) | EF) / Matching can be both a string "EF" or a string "ab" or "CD" once or more repetition.
In the regular expression, the second use of parentheses is to define sub-mode in a complete mode. When a regular expression is successfully matched with the target string, you can match the sub-pattern in the target string and parentheses.
Part. For example, assume that the mode we are searching is one or more letters following one or more numbers, then we can use mode / [AZ] / d /. But because we are truly concerned about every match
The number of the tail, then if we put the figure's number part in parentheses (/ [az] (/ d ) /), we can extract numbers from any match you search, then we will do it. F.
Another use of a sub-expression of bursts is to allow us to reference the previous sub-expression after the same regular expression. This is achieved by a string / post plus one or more numbers. Number finger Incense
The position in the regular expression. For example: / 1 is a sub-expression of the first stringed bracket. / 3 Reference is a sub-expression of the third stringed bracket. Note that due to the child expression Nested in other sub-expression,
Therefore, its position is the position of the left parentheses. For example, the following regular expression is specified as / 2: / ([JJ] AVA ([SS] CRIPT)) / SIS / S (FUN / W *) /
The reference to the previous sub-expression in the regular expression is not the pattern of that sub-expression, but the text that matches the pattern. This way, the reference is not just helping you enter the regular expression of the regular expression. fast
In shortcomings, it also implemented an agreement, which is a string of each separated portion contains exact same characters. For example: The following regular expression matches all words located within single quotes or double quotes
But, it requires the starting and end of quotation marks (for example, both are double quotes or single quotes): / ['"[^'"] * ['"] / if required to start and end quotes Match, we can use the following reference: / (['"]) [^'"] * / 1 /
/ 1 Matching the pattern of the sub-expression of the first burst. In this example, it implements a specification, that is, the starting quotation must match the quotes. Note that if the backslash Post-followed digital ratio
The number of sub-expression models is large, then it will be parsed into a decimal escape sequence, not a reference. You can adhere to the complete three characters to represent the escape sequence, this can avoid confusion. .E.g,
Use / 044, not / 44. The following is the selection, packet, and reference character of the regular expression:
Character Meaning ____________________________________________________________________ | select either sub-expression match the symbol to the left of either sub-expression to its right (...) grouping several items into a single unit by the unit *, ,?... And | Use the symbols, but also remember the characters that match this group for this
Using characters / n and the n-th packet is matched match packet is a subexpression brackets (possibly nested) The packet numbers are counted from left to right from the left bracket ____________________________________________________________________
5. Specify the location of the match
We have seen that many elements in a regular expression can match a character of the string. For example: / s matches just a blank character. There are also some regular expressions that match the width between characters.
0 Space, rather than actual characters, for example: / b Matching a boundary of a word, that is, the boundary between a / w word character and one / W non-character characters. The characters like / b are not specified Any one matched
The characters in the string are specified by the legal position that occurred in the match. Sometimes we call these elements as the anchor of the regular expression. Because they locate a specific location in the search string. Most commonly used anchor
It is ^, which makes the pattern depend on the beginning of the string, and the anchor element is positioned to position the mode at the end of the string.
For example: To match "javascript", we can use regular expressions / ^ JavaScript $ /. If we want to retrieve the word "java" itself (unlike in "javascript", then we can make
Modes // s Java / S /, it requires space before and after word Java. But this is two problems. First: If "Java" appears in the beginning of a character or end. This mode is Will not match it, except
There is a space at the beginning and end. Second: When this mode finds a matching character, the matching string front end and the rear end of the matching string have space, which is not what we want. So, We use words
The boundary / b is used instead of the true spaces / s match. The result expression is // b java / b /. The following is an anchor character of the regular expression:
Character meaning ____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ . Briefly word boundary is located between the character / w and / w (Note: [/ b] match is backspace). non-character words / B attributes matching a boundary _____________________________________________________________________6
The syntax of the regular expression also has the last element, which is the property of the regular expression, which shows the rules of advanced mode matching. Unlike other regular expressions, the attribute is explained outside the / symbol.
They do not appear between the two slashes, but after the second slash.javaScript 1.2 supports two properties. Properties i Description Matches should be case sensitive. Properties g Description Matches should be globally .and also
That is, you should find all the matches in the retrieveful string. These two properties can be found together to execute a global, case-sensitive matching.
For example: To perform a size insensitive search to find the first specific value of the word "java" (or "java", "java", we can use the size of the unoccupied regular expression // b java / B / I. If you want
Find all the specific values of "Java" in a string, we can also add attribute G, ie // b Java / B / GI.
The following is the properties of the regular expression:
Character meaning ___________________________i Execute Matching G executes case-sensitive match G performs a global match, in short, it is to find all matching, not after finding the first one, stop _______________________________________________________________________________________________________________________________________________________________________________________________
In addition to attributes G and I, the regular expression is not the same characteristic as the properties. If the configuration function regexp is set to true, the mode match will be done in multiple lines.
In mode, the anchor characters ^ and $ match are not just retrieving the beginning and end of the string, and also match the beginning and end of the row inside the string. For example: mode / java $ / match is "java", but Mismatch
"Java / NIS FUN". If we set up the multiline property, then the latter will also match:
Regexp.multiline = true;