Javascript Regular Expression (1) -- syntax

Source: Internet
Author: User
Tags character classes

The RegExp class in js represents a regular expression and has powerful pattern matching, text retrieval, and replacement functions. The Regular Expression Pattern rules are composed of a character sequence, including all letters and numbers. Most of the characters are matched according to the direct amount, some special characters do not match characters such as $ according to the literal meaning. When matching according to the non-literal meaning, you can use the backslash escape to create a RegExp object in the following two ways: eg: the following two methods can match the string ending with s // method 1 ($ matches the end of the string), using the constructor var pattern = new RegExp ("s $ "); // Method 2: Use a regular expression to define the character var pattern =/s $/between a slash. Note: when a regular expression in ECMAScript3 is executed, it is converted to a RegExp object, each operation of the regular expression expressed in the same code returns the same object (different from {} and [], a new object is created each time ), in ECMAScript5, The RegExp object in ECMAScript3 will share the same instance. Cript5 contains two independent instance characters: 1. Copy code letters and numbers directly: \ o: NUL character \ t: tab \ n: linefeed \ v: vertical tab \ f: Paging character \ r: carriage return .... copy code 2. character classes put a direct amount of characters in square brackets to form a character class. A character class can match any character contained by it. For example: /[abc]/the list of a, B, and c character classes that can be matched is as follows: copy the Code [...]: any character in square brackets [^...]: any character not in square brackets.: any character except line break and other Unicode line terminator \ w: A word consisting of any ASCII character, equivalent to [a-zA-Z0-9] \ W: Any word that is not suitable for ASCII characters, equivalent to [^ a-zA-Z0-9] \ s: Any Unicode space character \ S: any non-Unicode space character \ d: any ASCII number, equivalent to [0-9] \ D: equivalent to [^ 0-9] [\ B]: duplicate code copying Code directly out of the lattice/\ d/can match two numbers. What should I do if there are many digits? The following method can be used: Copy code {n }: match the previous item n times {n ,}: match the previous item n times or more times {n, m}: match the previous item at least n times, at most m times *: match the first item 0 or multiple times. It is equivalent to {0,} +: match the first item once or multiple times. It is equivalent to {1 ,}?: Match the first item 0 times or 1 time (that is, the first item is optional) is equivalent to copying code {0, 1} above this method is "greedy" match, that is, as many matches as possible. If you want a non-Greedy match, you can add a question mark after the matched character. Eg:/a +/AND/a +? /. If aaa is used as a matching string, the previous method will match aaa, the latter method only matches the first a. When you need to match aaab,/a + B/AND/a +? B/equivalent grouping character | used to separate selected characters, such as eg/AB | cd | ef/can match string AB, cd, or ef. The matching order of the selected items is from left to right. If the selected items on the left are matched, the matching items on the right are ignored, eg/a | AB/matching AB can only match a parentheses. combine individual items into expressions such as:/java (script )? /Can match java or javascript 2. define the sub-mode in the completion mode. When a regular expression is successfully matched with the target string, you can extract the part matching the sub-mode in parentheses from the target string. For example: /[a-z] + (\ d +)/in the mode/[a-z] + \ d +/one or more lower-case letters can be matched with one or more numbers, then we can use () to enclose \ d + and extract the numerical part from the matching item. This method can be used when we are more concerned with the following numbers. 3. allow reference to the previous subexpression after the same regular expression. By adding one or more numbers after the backslash \, this number specifies the position of the subexpression with parentheses in the regular expression eg1:/(java (script) [abc] +)/medium (script) can use \ 2 to refer to eg2: /['"] [^'"] * ['"] can be used/(['"]) [^ '"] * \ 1/alternative (? :....)(? :) Combine items into a unit, but do not generate references. Different from the sub-expression () eg: In/([Jj] ava (?: [Ss] callback )?) \ Sis \ s (fun \ w *)/medium \ 2 and (fun \ W *) Match anchor (specify a matching position) an anchor is used to specify a valid location for matching expressions. The most commonly used anchor element is ^ used to match the start of a string. Anchor element $ is used to match the end summary list of a string: copy the code ^: matches the beginning of a string, multi-row search is used to match the beginning of a row $: match the end of a string, and multi-row search is used to match the end of a row \ B: match the boundary of a word (different from [\ B] backspace) \ B: match the position of a non-word boundary (? = P): Forward-first assertions. The following characters match p (?! P): indicates a negative first assertion. If the subsequent characters do not match p, copy the Code. For example, positive first assertion and negative first assertion: eg1: /[Jj] ava ([Ss] Ghost )? (? = \:)/The modified mode can match Javascript in Javascript: is a very interesting language, but cannot match Java eg2:/Java (?! Script) [A-Z] \ w */can match Java followed by an uppercase letter and any number of ASCII words, but it cannot follow the Script. For example, it can match JavaBeans and cannot match the Javanese modifier. It is placed outside of "/". The modifier below can be any combination I: The matching mode is case insensitive. g: it indicates that all matches should be found when the matching mode is global, instead of stopping the m: multi-row execution mode when the first one is found. At this time, ^ and $ not only match the start and end of the entire string, you can also match the beginning and end of each line. For example:/\ bjava \ B/I, You can match java Java JAVA and other/JAVA $/im. For java Java \ nis fun, note 1. the content enclosed in double quotation marks is not allowed in regular expressions, and vice versa. 2. the quote of the word expression eg:/(['"]) [^ \ 1] * \ 1/is invalid in character classes.

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.