"RegExp" javascript normal expression judgment matching rules and common methods

Source: Internet
Author: User

Strings are the most data structure involved in programming, and the need to manipulate strings is almost ubiquitous. A regular expression is a powerful weapon used to match strings. It is designed to define a rule for a string in a descriptive language, and any string that conforms to the rule, we think it "matches". \dCan match a number ‘00\d‘Can match ‘007‘‘\d\d\d‘Can match ‘010‘ \wCan match a letter or number ‘\w\w‘Can match ‘js‘ \sCan match a space (also including tab and other whitespace characters) \DWSMatches the matching relationship with the lowercase letter. \数字nRepresents a reference to the text that is captured by grouping N, which can be matched again /(\d+)(0*)\1/Can match ' 22300223 ' .can match any character ‘js.‘Can match ‘jsp‘‘jss‘‘js!‘ *Represents any character (including 0) +Represents at least one character ?Represents 0 or 1 characters {n}Represents n characters {n,m}Represents a n-m character A|BCan match A or b (J|j)ava(S|s)criptCan match ‘JavaScript‘‘Javascript‘‘javaScript‘Or ‘javascript‘ ^Represents the beginning of a row ^\dIndicates that a number must begin $Indicates the end of a row \d$Indicates the need to end with a number []Representation Range []In the ^means to exclude a character [^#?]Except for that? and # all the characters [a-zA-Z\_\$][0-9a-zA-Z\_\$]*Can be matched by a letter or underscore, a $, followed by a string consisting of a number, letter, or underscore, which is the variable name allowed by JavaScript [a-zA-Z\_\$][0-9a-zA-Z\_\$]{0, 19}More precisely limit the length of a variable to 1-20 characters (1 characters + 19 characters before) using regular expressions to slice a string more flexibly than a fixed character
' A, b;; C  d '. Split (/[\s\,\;] +/); [' A ', ' B ', ' C ', ' d ']

  

In addition to simply judging whether a match is matched, the regular expression also has the power to extract substrings. Use ()Represents the group to extract if a group is defined in a regular expression, you can RegExpObject with the exec()method to extract the substring from the string. exec()After the match succeeds, returns a Array, the first element is the entire string to which the regular expression matches, followed by a string that represents the successful substring. exec()return if match fails null^(\d{3})-(\d{3,8})$Two groups are defined separately, and the area code and local numbers can be extracted directly from the matching string:
var re =/^ (\d{3})-(\d{3,8}) $/;re.exec (' 010-12345 '); [' 010-12345 ', ' 010 ', ' 12345 ']re.exec (' 010 12345 '); Null

  

of the RegExp object test()method is used to test whether a given string conforms to a condition.
var re =/^\d{3}\-\d{3,8}$/; re.test (//  true//  false

It should be noted that the regular match is the default Greedy Match, which is to match as many characters as possible.
var re =/^ (\d+) (0*) $/; re.exec (//  [' 102300 ', ' 102300 ', ']

Because \d+Using greedy match, directly to the back of the 0All matched, the result 0*Only empty strings are matched.

Add one to ? make a \d+ non-greedy match:

var re =/^ (\d+?) (0*) $/; re.exec (//  [' 102300 ', ' 1023 ', ' xx ' )
?: Do not want to be captured when used, can improve the program execution speed such as ([a-z][0-9]) +, () inside the content is captured, if written (?: [a-z][0-9]) +, the overall match is the same, is not capturing () content. Special signs gFlag that represents the global match iFlag, indicating ignoring case mFlag, indicating that multiple line matching is performed

"RegExp" javascript normal expression judgment matching rules and common methods

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.