Form validation-Regular expressions

Source: Internet
Author: User

A regular expression is a powerful tool that can be used for pattern matching and substitution, and is a validation of data validity.
First, the basic grammar

    位于“/”定界符之间的部分就是将要在目标对象中进行匹配的模式。用户只要把希望查找匹配对象的模式内容放入“/”定界符之间即可。为了能够    使用户更加灵活的定制模式内容,正则表达式提供了专门的“元字符”。所谓元字符就是指那些在正则表达式中具有特殊意义的专用字符,可以用来    规定其前导字符(即位于元字符前面的字符)在目标对象中的出现模式。
  1. The more commonly used metacharacters are: "+", "*", and "?".
    The "+" metacharacters stipulate that their leading characters must appear one or more times in the target object.
    The "*" meta-character specifies that its leading character must appear 0 or more times in the target object.
    “?” A meta-character specifies that its leading object must appear 0 or more times in the target object.
    Cases:
    /fo+/because the regular expression contains the "+" metacharacters, it is possible to match the string "fool", "fo", or "football" in the target object with one or more consecutive letters o after the letter F.
    /eg*/because the regular expression contains the "*" metacharacters, it can be matched with a string of "easy", "ego", or "egg" in the target object, such as 0 or more occurrences of the letter G after the letter E.
    /wil?/because the "?" is included in the preceding regular expression Metacharacters, which represents a string that can be matched with "Win" in the target object, or "Wilson", and so on, after the letter I consecutively appear 0 or one letter L.

  2. Sometimes you don't know how many characters to match. To be able to adapt to this uncertainty, the regular expression supports the concept of qualifiers. These qualifiers can specify a given component of a regular expression
    How many times must be present to satisfy the match.
    {n} n is a non-negative integer. Matches the determined n times. Example ' o{2} ' cannot match ' o ' in ' Bob ', but can match two o in ' food '.
    {N,} n is a non-negative integer. Match at least n times. Example ' o{2,} ' cannot match ' o ' in ' Bob ', but can match all o in ' Fooood '. ' O{1,} ' is equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '.
    {n,m} m and n are non-negative integers, where n<=m. Matches at least n times and matches up to M times. Example "o{1,3}" will match the first three o in "Foooood". ' o{0,1} ' is equivalent to ' O? '
    Note that there can be no spaces between a comma and two numbers.

    3. How to use several important metacharacters:

    \s: Used to match a single space character, including Tab key and line break;
    \s: Used to match all characters except a single space character;
    \d: Used to match numbers from 0 to 9;
    \w: Used to match letters, numbers, or underscore characters;
    \w: Used to match all characters that do not match the \w;
    . : Used to match all characters except the line break.

    (Note: We can think of \s and \s as well as \w and \w as inverse for each other)
    /\s+/the preceding regular expression can be used to match one or more space characters in the target object.
    /\d000/If we have a complex financial statement in hand, we can easily find all sums amounting to thousands of dollars through these regular expressions.

4. In addition to the meta-characters we have described above, there is another unique special character in the regular expression, the locator. The locator is used to specify the matching pattern in the target object
Where it appears in the. The more commonly used locators include: "^", "$", "\b", and "\b".

    “^”定位符规定匹配模式必须出现在目标字符串的开头    “$”位符规定匹配模式必须出现在目标对象的结尾    “\b”定位符规定匹配模式必须出现在目标字符串的开头或结尾的两个边界之一    “\B”定位符则规定匹配对象必须位于目标字符串的开头和结尾两个边界之内,即匹配对象既不能作为目标字符串的开头,也不能作为结尾。    同样,我们也可以把“^”和“$”以及“\b”和“\B”看作是互为逆运算的两组定位符。    举例来说: /^hell/ 因为上述正则表达式中包含“^”定位符,所以可以与目标对象中以 “hell”, “hello”或“hellhound”开头的字符串相    匹配。 /ar$/ 因为上述正则表达式中包含“$”定位符,所以可以与目标对象中以 “car”, “bar”或 “ar” 结尾的字符串相匹配。    /\bbom/ 因为上述正则表达式模式以“\b”定位符开头,所以可以与目标对象中以 “bomb”, 或 “bom”开头的字符串相匹配。    /man\b/因为上述正则表达式模式以“\b”定位符结尾,所以可以与目标对象中以 “human”, “woman”或 “man”结尾的字符串相匹配。

Description

为了能够方便用户更加灵活的设定匹配模式,正则表达式允许使用者在匹配模式中指定某一个范围而不局限于具体的字符。例如:/[A-Z]/上述正则表达式将会与从A到Z范围内任何一个大写字母相匹配。/[a-z]/上述正则表达式将会与从a到z范围内任何一个小写字母相匹配。/[0-9]/  上述正则表达式将会与从0到9范围内任何一个数字相匹配。/([a-z][A-Z][0-9])+/ 上述正则表达式将会与任何由字母和数字组成的字符串,如 “aB0” 等相匹配。这里需要提醒用户注意的一点就是可以在正则表达式中使用 “()” 把字符串组合在一起。“()”符号包含的内容必须同时出现在目标对象中。如果我们希望在正则表达式中实现类似编程逻辑中的“或”运算,在多个不同的模式中任选一个进行匹配的话,可以使用管道符 “|”。正则表达式中还有一个较为常用的运算符,即否定符 “[^]”。与我们前文所介绍的定位符 “^” 不同,否定符 “[^]”规定目标对象中不能存在模式中所规定的字符串。例如:/[^A-C]/上述字符串将会与目标对象中除A,B,和C之外的任何字符相匹配。一般来说,当“^”出现在 “[]”内时就被视做否定运算符;而当“^”位于“[]”之外,或没有“[]”时,则应当被视做定位符。最后,当用户需要在正则表达式的模式中加入元字符,并查找其匹配对象时,可以使用转义符“\”。例如:/Th\*/上述正则表达式将会与目标对象中的“Th*”而非“The”等相匹配。

Ii. Examples of Use
There is a powerful regexp () object in JavaScript 1.2 that can be used to match the regular expression. The test () method can verify that the target object is
Contains the matching pattern, and returns TRUE or false accordingly.

语法: re = new RegExp("pattern",["flags"])re——必选项。将要赋值为正则表达式模式的变量名。 pattern——必选项。要使用的正则表达式模式。flags——可选项。要用引号将 flag 引起来。标志可以组合使用,可用的有:g (全文查找出现的所有 pattern)   i (忽略大小写)   m (多行查找)

Verify Number: ^[0-9]*$ Verify N-bit number: ^\d{n}$ validates at least n digits: ^\d{n,}$ verifies the number of m-n bits: ^\d{m,n}$ verify 0 and non 0 numbers starting with: ^ (0|[1-9][0-9]*) $ verifies that there is a positive real number with two decimal places: ^[0-9]+ (. [ 0-9]{2})? $ Verify that there is a positive real number with 1-3 decimal places: ^[0-9]+ (. [ 0-9]{1,3})? $ verify non-zero positive integers: ^\+? [1-9][0-9]*$ validation of nonzero negative integers: ^\-[1-9][0-9]*$ Validation of nonnegative integers (positive integers + 0) ^\d+$ validation of non-positive integers (negative integers + 0) ^ ((-\d+) | ( 0+) $ to verify the character length 3: ^. {3}$ validates a string of 26 English letters: ^[a-za-z]+$ validates a string of 26 uppercase English letters: ^[a-z]+$ validates a string consisting of 26 lowercase English letters: ^[a-z]+$ validates a string consisting of a number and 26 English letters: ^[ a-za-z0-9]+$ validates a string consisting of numbers, 26 letters, or underscores: ^\w+$ Verify user password: ^[a-za-z]\w{5,17}$ the correct format is: Start with a letter, the length is between 6-18, and can contain only characters, numbers, and underscores. Verify that it contains ^%& ',; =?$\ "characters:[^%& ',; =?$\x22]+ Verify kanji: ^[\u4e00-\u9fa5],{0,}$ Verify email Address: ^\w+[-+.] \w+)*@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$ authentication interneturl:^http://([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*) $; ^[a-za-z]+://(w+ (-w+) *)(. (w+ (-w+)*)) *(? s*)? $ authentication Phone number: ^ (\ (\d{3,4}\) |\d{3,4}-)? \d{7,8}$:--the correct format is: xxxx-xxxxxxx,xxxx-xxxxxxxx,xxx-xxxxxxx,xxx-xxxxxxxx, Xxxxxxx,xxxxxxxx. Verifying a Social Security number (15-bit or 18-digit number): ^\d{15}|\d{}18$ validation 12 months of the year: ^ (0?[ 1-9]|1[0-2]) $ correct format for: "01"-"09" and "1" "12" verify one-month 31 days: ^ (0?[ 1-9]) | ((1|2) [0-9]) |30|31) $ The correct format is: 01, 09 and 1, 31. Integer: ^-?\d+$ non-negative floating-point number (positive float + 0): ^\d+ (\.\d+)? $ positive floating-point number ^ ([0-9]+\. [0-9]*[1-9][0-9]*)| ([0-9]*[1-9][0-9]*\. [0-9]+)| ([0-9]*[1-9][0-9]*) $ non-positive floating-point number (negative floating-point number + 0) ^ ((-\d+ (\.\d+)?) | (0+ (\.0+)?)) $ negative floating-point number ^ (-([0-9]+\. [0-9]*[1-9][0-9]*)| ([0-9]*[1-9][0-9]*\. [0-9]+)| ([0-9]*[1-9][0-9]*))) $ floating point ^ (-?\d+) (\.\d+)?

Form validation-Regular expressions

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.