Regular expression is an object describing character mode.
JavaScript's regexp object and String object define a method of using 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, You can use a regexp () constructor to create a regexp object, or a newly added special syntax in JavaScript 1.2 can be used to create a regexp object. Like a string direct volume is defined as characters contained in quotation marks, regular expression The direct amount is also defined as a character between a pair of slash (/). So, JavaScript may contain the following code: var pattern = / s $ /; this line code creates a new regexp object, and Get it to the variable parttern. This special regexp object matches all strings ending with the letters "S". Use regexp () or 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 relatively easy. More difficult tasks is to describe characters in the characterization of characters with regular expression syntax. JavaScript The PERL language regular expression syntax is a fairly complete subset. The mode specification of the regular expression is made up of a series of characters. Most numbers (including all alphanumeric characters) are matched according to the literal meaning. Character. Say, regular expression / java / just match all strings containing substrings "java". Although other characters in the regular expression are not matched according to literal meaning, they all have special meaning Regular expression / s $ / contains two characters. The first special character "S" is the same as it matches itself. The second character "$" is a special character, what matches the string End. So regular expression / s $ / match is the string ending with the letter "S". 1. Direct quantum character we have discovered that all alphabet characters and numbers in the regular expression are all according to literality The regular expression of itself matching. JavaScript also supports certain non-alphabetic characters by the escape sequence starting with the backslash (/). For example, the sequence "/ n" matches a direct quantity wrap in the string In the regular expression, many punctuation symbols have special meanings. The following is these characters and their meaning: regular expressions Direct quantity character character matching ___________________________________________ One. Direct / * One * Direct Quantity / One
Delinitile /? One? Direct Quantity / | One | Direct Quantity / (One (Delivered /) One) Delin / [One [Dip /] One] Delip / {One {Delive /} One} Direct Quantity / Xxx 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 a special punctuation symbol in the regular expression, you must add a "/" before them, and the character class will be combined into a character class in the middle brackets. A character class and it Any of the characters contained matches, so regular expression / [ABC] / and letters "A", "B", "C"
Any one of the mids. You can also define a negative character class, which match all characters that are included in the characters contained within the brackets. Define the negative character tip, you want to use a ^ symbol as from the left The first character of parentheses. 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. these classes are used to represent, for example, / s is matched spaces, tabs, and other whitespace, / s matches any character other than the whitespace. regular table ash formula character-class character match ____________________________________________________ [...] Any character [^ ...] located within parentheses, any character among parentheses. In addition to any character outside of the newline character, equivalent to [^ / n] / W any single character, etc. Price from [A-ZA-Z0-9] / W any non-single character, equivalent to any blank character [^ a-za-za-z0-9] / s, equivalent to [/ t / n / r / f / V] / s Any non-blank character, equivalent to [^ / t / n / r / f / v] / d any number, equivalent to any character outside the number, equivalent On [^ 0-9] [/ b] a back direct volume (special case) _____ ______________________________________________________________________ 3. Copy the above regular expression syntax, you can describe / / D / D / D / D / D / D / D / D / D / D / D / D /. But we have no one Methods can be used to describe numbers with any number of digits or a string. This string consists of three characters and a number of numbers following the letters. These complex patterns use the regular expression syntax specified in this expression. The number of times each element is repeated. The specified character always appears behind the mode they act. So some special characters are specifically used to represent them. For example: number matching It is a mode in which the previous mode is replicated once or multiple times. The following table lists the copy syntax. First see 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. / [^ "
] * / // matches zero or more non-quote characters. Regular expressions copied characters in the character meaning __________________________________________________________________ {n, m} before a match at least n times, but not more than m times {n,} matches the previous Item n times, or multiple times {n} matches the previous one just n times? Match the previous 0 times or 1 time, that is, the previous item is optional. Equivalent to {0, 1} match One or more, equivalent to {1,} * matches the previous 0 times or more. Equivalent to {0,} ______________________________________________________ 4. Select, Group, and Quote Regular Expression Syntax also includes specified selection Item, pair sub-expression packets and special characters of the previous child expression. Characters | are used to separate the selected characters. For example: / ab | cd | EF / match is string "ab", or string "CD", or "ef". // D {3} | [AZ] {4} / match is either a three-digit number, or four lowercase letters. Several brackets in regular expressions Role. Its main role is to form a separate item into a sub-expression in order to handle those items like *, or?, For example: / java (script)? / Match is String "java", thereafter can be "script" or no. / (AB | CD) | EF) / match can be both a string "EF" or a string "ab" or "CD" once or more repetition. In regular expression, parentheses The second use is to define sub-mode in a complete mode.
When a regular expression is successfully matched with the target string, it is possible to extract the part of the sub-pattern in the target string, for example, suppose the mode we are searching is one or more letters followed by one Or multiple numbers, then we can use mode / [AZ] / d /. But because we really care about the number of each matching tail, then we put the pattern of the figure in parentheses (/ [AZ ] (/ d ) /), we can extract numbers from any match you have retrieved, then we will parse this. Another use of a sub-expression of a burst is to allow us to be in the same regular basis. The expression is referenced later with the previous sub-expression. This is achieved by a string / post plus one or more digits. The number refers to the position of the sub-expression in the regular expression. For example: / 1 is a sub-expression of the first burst. / 3 Reference is a sub-expression of the third burst. Note that due to the sub-expression, it can be nested in other sub-expression, so it's It 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 *) / pair regular expression The reference to the previous sub-expression is not the pattern of the sub-expression, but the text that matches the pattern. This way, the reference is not just the shortcut to the repetitive part of the regular expression. It also implemented an agreement, that is, a string of each separated portion contains the identical characters. For example: The following regular expression matches all characters located within single quotes or double quotes. However, it The quotation marks that start and end (eg, both are double quotes or single quotes): / ['"[^'"] * ['"] / if the quotation number started and ended, we can use The following reference: / (['""]) [^' "] * / 1/1 matches the mode matching the first stringed sub-expression. In this example, it implements a specification That is the beginning of the quotes must match the end of the quotes. Note It is intended that if the number followed by the backslash is much more than the number of brackets, then it will be parsed into a decimal escape sequence, not a reference. You can adhere to the complete three characters escape sequences, which are to avoid confusion, for example, use / 044 instead of / 44 the following is a selection of regular expressions, grouping, and the quote character: character meaning ____________________________________________________________________ | select either matching the symbol on the left Sub expressions, either the sub-expression (...) on the right. Several items are divided into a unit. This unit can be *, ,?
And | Use symbols, and you can also remember to match the characters that match this group for this later, the quote matching characters that use / n and the nth packets. Packet is the sub-expression in parentheses (may be nested .) packet number is counted from left to right from the left bracket 5. specify the location ____________________________________________________________________ match we have seen, a regular expression of many elements to be able to match a character string, for example:. / s match It's just a blank character. There are also some regular expressions that match the space between the characters 0, not the actual character, for example: / b matches the boundary of a word, that is, one / w characters And a boundary between a / W non-character. The characters like / b do not specify any characters in any matching string, which specifies the legal position that occurred in the match. Sometimes we call these elements as regular Expression anchor. Because they position mode in a specific location in the retrieval string. The most commonly used anchor element is ^, which depends on the beginning of the string, and the anchor element is positioned to locate the string At the end. 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 use Mode // s Java / S /, it requires a space before and after word Java. But do two problems. First: If "java" appears in the beginning of a character or end. This mode is not It will match it unless there is a space at the beginning and end. Second: When this pattern finds a matching character, the matching string front end and the rear end of the matching string have space, this is not what we think To. Therefore, we use the boundary / b of the words to match the true space characters / s. The result expression is // b java / b /. The following is an anchor character: Character meaning ___________________________________________ ^ Matching the beginning of the character, in multi-line search, matching is the beginning $ of a row, the end of the character, in the multi-line search, matching the end / B matching is a boundary of a word. Simply In other words, the position between characters / w and / w (Note: [/ b] matches the escapement) / b matching characters __________________________________________________________________________________________________________________________________________________________________________________________ 6. Syntax related to regular expressions There is also the last element, that is, the attribute of the regular expression, which shows the rules of the advanced mode match. Unlike other regular expression syntax, the attribute is illustrated outside the / symbol. That is, they do not appear in two oblique Between the bar, but in the second slash. JavaScript 1.2 supports two properties. Properties i Description Matches should be case sensitive. Properties g Description Mode match should be global. That is to say, you should find All matching strings in the retrieval. These two properties can be implemented to perform a global, case-sensitive matching. For example: To perform a size insensitive search to find the word "java" (or The first specific value of "Java", "Java", etc., we can use the size insensitive regular expression // b java / b / i. If you want to find "Java" in a string
All specific values, we can also add attribute g, ie // b java / b / g. The following is an attribute of the regular expression: character meaning ___________________________i Execute Matching G executes a global match, simply In other words, it is found to find all the matching, not after the first one will stop ______________________________________________________________________________________________________________________________________________________________________________________________________ 正 正 表 表 表 表 表 表 表 表 正 表 正 正 正 表 表 正 正 正 正The attribute multiline is set to TRUE, then the mode match will be performed in multi-line mode. In this mode, the anchor character ^ and $ match is not just retrieving the beginning and end of the string, and also match the beginning of the inside of the string And ending. For example: mode / Java $ / match is "java", but do not match "Java / NIS FUN". If we set up the multiline property, then the latter will also match: regexp.multiline = true; Reprint To: http://blog.9cbs.net/xiaofanku/archive/2004/09/26/117106.aspx