Fourth article, JavaScript

Source: Internet
Author: User
Tags uppercase character

first, Regular Expression

Tip: This topic requires multiple rounds of review and repeated deepening and understanding

Two ways to use regular expressions:

1) regexp.xxx (string);

2) string.yyy (regexp);

Verify that the phone number format entered by the       user is legal var regexp=/^1[3-8]\d{9}$/;         var input="15012345678";        Console.log (regexp.test (input))

Regular expressions are primarily used for form validation

1. Direct Volume characters

Defines a regular expression object that can use Perl-style direct-volume syntax

/pattern/attributes

Description

Direct amount string cannot be added ' or ' (quotation marks) around

Two/not omitted

Parameter pattern is a regular expression string

The parameter attribute is an optional string that can be specified in the range "g", "i" and "m", respectively, to specify global matching, case-sensitive matching, and multi-line matching

2. Character class

All individual uppercase and lowercase letters, numbers are a regular expression that matches a single character, which is the same as itself

        var regexp=/ipod/;         var data='Apple ipod is no.123 cool? ' ;        Console.log (regexp.test (data)); // false        var regexp=/ipod/;         var data='Apple ipod is no.123 cool? ' ;        Console.log (regexp.test (data)); // true
View Code

Regular expressions some characters have special grammatical meanings that cannot be used directly, and must be escaped with \ to be used

. \ / * ? + [ ( ) ] { } ^ $ |

Location matching

^ In/[^123]/matches except for 123

A/^1[123]/that contains 123 and starts with 1.

$ indicates what ends with

| The/[123|456]/represents a match of 123 or 456 of one

A/[123x,]/that matches any of the 123x

\b Indicates the bounds of the matched character

\b The non-boundary example of a matching string: "he is the" so "if it matches his/[/bhis/b in history"

3. Repetition

In regular expressions, you can use the following special characters to define how frequently characters appear---quantifier metacharacters

repeating character   meaning                                  example n?        match 0 or once characters N           /a?/N*         matches 0 or more characters n           /a*/n+        matches once or multiple characters n            /a+/n{x}     match character n occurs x times                 /a{3}/   n{x,y}  match character n appears x to y times              /a{2,4}/    n{x,}    matches the occurrence of the character N >=x times          /a{3,}/

4. selection, Grouping and referencing

Range selection, which specifies a matching range

syntax meaning example [abc] matches any one of the characters in the specified collection/[3458]/[^abc] matches any character that is not in the specified collection/[^12679]/[a-z] matches any lowercase character/[a-z]/[0-9] matches any number/0-9/[A-z] matches any uppercase character/a-z/[A-z] matches all characters of uppercase A through association z,/[a-z]/is a-z[\]^_ ' a-z/[--to]/Exp|EXP2 using | for conditional selection/ex| Ex|post| post/(expl) use () to specify grouping------sub-expression

A regular expression can be simplified by using the following metacharacters reference

 1   syntax meaning example /span>2  \d matches a number/\d/equivalent to/[0
     -9 ]/3  \d matches a non-numeric/\d/, etc. Price At/[^0 -9 ]/ 4  \w matches a number/letter/underscore/\w/equivalent to/[0  -9a-za-z]/ 5  \w matches a non-numeric/character/underscore/\w/equivalent to/[^0  -9a-za-z] /6  \s match a white space character (space)/\s/equivalent To/[\n\r\t\v\f]/7
       \s matches a non-whitespace character/\s/equivalent to/[^\n\r\v\t\f]/8 . Matches any single character except carriage return and line break/./equivalent to/[^\n\r]/

Grouping matches, using () to make a whole number of characters, each of which will automatically get 1, 2, 3 、... Such a grouping

()/(abc) {3}/represents A group of 3 matching groups with ABC

/(abc) (xvv) (xx) 2/mean match (abc) (xvv) these two groupings

/(aa) (bb) (cc)/2/represents A match (bb) for this group

5. Property modifiers in regular expressions

/expression Content/property modifier

The property modifier can be selected within the following three ranges

1) I match ignore case

2) G performs a global match, finds a match and continues, knowing the end

3) M performs a multi-sweat match that affects the meaning of ^ (originally only matches the beginning of the entire String) and $ (which originally only matches the end of the entire string), matching the beginning and end of each line

Fourth article, JavaScript

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.