JS Regular Expression usage Summary

Source: Internet
Author: User
Method and attribute related to the regular expression: Regexp object method: Test, returns a bool value, which indicates whether the pattern exists in the searched string. If trueis returned, the system returns false.exe C, runs the search in the string in regular expression mode, and returns an array containing the search result. Compile: Compile the regular expression into an internal format for faster execution. Regexp object attribute: source, returns a copy of the text in regular expression mode, read-only. Lastindex returns the character position, which is the starting position of the next successful match in the searched string. $1... $9, returns the string found during the nine pattern matches, read-only. Input ($ _), returns the string to be searched according to the execution specification, read-only. It is best to use Regexp ['$ _'] lastmatch ($ &) in this way to return the final matched characters in any regular expression search, read-only. Lastparen ($ +), if any, returns the final child match in any regular expression search process, read-only. Leftcontext ($ '), returns the character between the string start position and the last position before matching, read-only. Rightcontext ($ ') returns the character from the last matching position to the end of the string. Read-only. Some methods of the string object related to the regular expression: Match, find the matching of one or more regular expressions. Replace: Replace the substring that matches the regular expression. Search to retrieve the value that matches the regular expression. Split: splits the string into a string array. VaR Re = new Regexp ('A'); var Re = new Regexp ('A', 'I'); the first parameter of the Regexp constructor is the text content of the regular expression, the second parameter is an optional flag, which can be used in combination with: G, full-text search I, case-insensitive m, and multiple-row search to create a regular expression can also be abbreviated: vaR Re =/\ D +/GI; All metacharacters: escape the metacharacters when they are used. ([{\ ^ $ | )? * +. Greedy match: Re =/(\ D +) 0/; indicates matching 10,100,100, and so on. Inert match: Re =/(\ D + )? 0)/; indicates 10 matches, and returns the minimum match. Capture group, add (): Re =/(\ D +) ([A-Z])/; non-capture group, add?: Re =/(\ D + )(?: [A-Z])/; candidate | Re =/(if | else) \ D/; matches if 3 or else 5 nested groups: (? (B? (C ?))) Multiple external groups are generated in three groups. Reverse Reference Re =/(\ D +)-(\ D +)/; S = "12-34"; S. replace (Re, "$2-$1"); Re =/(\ W +) \ s \ 1/; matches go or how. Forward Looking forward :? = Re =/([A-Z] + )(? = \ S + 8 \. 10)/GI; S = "ubuntu 8.10"; S. Replace (Re, 'redtub') // The result is redtub 8.10 negative forward looking :?! Re =/([A-Z] (?! \ D)/I; STR = "ABC 1 one"; re. test (STR); alert (Regexp. $1); // The result is one complex example: Construct a regular expression to verify the validity of the email address. Email Address validity requirements: the user name can only contain letters, numbers, and underscores (_), at least one digit, at most 15 digits, followed by @, followed by a domain name, A domain name must contain only letters, numbers, and hyphens (-). It cannot start or end with a minus sign, followed by a domain name suffix (multiple domain names can be entered ), the domain name suffix must contain 2-4 English letters and periods. VaR Re =/^ \ W {1, 15 }(? :@(?! -))(? :(?: [A-z0-9-] * (?: [A-z0-9] (?! -))(? :\.(?! -) + [A-Z] {2, 4} $ /;


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.