JS regular-expression syntax

Source: Internet
Author: User
Tags lowercase regular expression

Regular expressions can help us better describe complex text formats. Once you have described these formats, you can use them to retrieve, replace, extract, and modify text data.

Here is a simple example of a regular expression. The first step is to introduce the namespaces for the regular type:

Using System.Text.RegularExpressions;

The second step is to construct a regular expression object with the specified regular, and the following regular is used to search the English letter of A-Z with a length of 10:

Regex obj = new Regex ("[a-z]{10}");

Finally, the match is retrieved in the specified data according to the regular formula, and if the matching IsMatch method returns True.

MessageBox.Show (obj. IsMatch ("Shivkoirala"). ToString ());

3 Important Regular commands

The best way to remember the regular grammar is to remember these three things: bracket (parentheses), caret (caret), and dollars (dollar sign).
There are 3 types of parentheses in the regular expression

Square brackets "[" and Braces "{".

The brackets "[" are the characters that need to be matched, and the number of matching characters is specified within the curly brace "{".

The parentheses "(") are used for grouping.

The caret "^" indicates the beginning of the regular formula.
The dollar sign "$" indicates the end of the regular formula.

Check if the user has entered the Shivkoirala?

Shivkoirala
Let's start with the first validation and enter the characters between a-g?

[A-g]
The characters entered are between a-g and the length is 3?

[A-g] {3}
The characters entered are between a-g and the maximum length is 3 minimum length of 1?

[A-g] {1,3}
How do I fit a fixed 8-digit number like 91230456, 01237648?

^[0-9]{8}$
How to verify that the minimum length is 3 of the maximum length of 7, such as: 123, 1274667, 87654?

^[0-9]{3,7}$
How do I verify an invoice number like LJI1020, the first 3 digits with a 8-bit length remaining in the letter?

The first three are the letters:

^[A-Z]{3}
followed by a 8-digit length number:

[0-9] {8}
So the whole expression is:

^[a-z]{3}[0-9]{7}$
Verify that the first 3 digits, like INV190203 or inv820830, are case-insensitive English letters, and the remaining 8 digits are numbers.

In the preceding expression, only the first 3 invoice numbers that are lowercase letters can be matched, and if we enter uppercase letters, we cannot match them. So to make sure that the first 3 letters are case-insensitive, we'll use the expression ^[a-za-z]{3}.

^[a-za-z]{3}[0-9]{7}$
Can we verify the simple URL format?

First step: Check for the existence of www:

^www.
The second step: the domain name must be the length of 1-15 in English letters:

. [A-z] {1,15}
Step three: End With. com or. org:

. (com|org) $
The complete expression is as follows:

^www[.] [A-z] {1,15} [.] (com|org) $
Let's look at how the BCD (in fact, the 3 basic syntax above) validates the email format.

The first step: email begins with a length of 1-10 in English letters, and finally with a "@":

^[a-za-z0-9]{1,10}@
Step two: @ followed by an English letter with a length of 1-10, followed by a "." :

[A-za-z] {1,10}.
Step three: End With. com or. org:

. (com|org) $
The final complete expression is as follows:

^[a-za-z0-9]{1,10}@[a-za-z]{1,10}. (com|org) $
Number of validated values in 0-25:

^ ([0-9]) | ([0-1][0-9]) | ([0-2][0-5]) $
The validation format is mm/dd/yyyy, YYYY/MM/DD and dd/mm/yyyy date:

Check the DD first. The length of DD is 1-29 (February), 1-30 (month small), 1-31 (month-old).

So DD is 1-9 or 01-09.

[1-9]|0[1-9]

Allows the user to enter 1-9 or 01-09.

Then add a match for DD 10-19

[1-9]|1[0-9]

Allows the user to enter 01-19.

Then add a match for DD 20-29

[1-9]|1[0-9]|2[0-9]

Allows the user to enter 01-29.

I again add a match for DD 30-31

[1-9]|1[0-9]|2[0-9]|3[0-1]

The last user can enter 01-31.

To match the separator "/", "-" for the day period.

[/ . -]

Allows the user to enter a date separator.

MM is similar to the operation

[1-9]|0[1-9]|1[0-2]

Let the user enter a month value of 01-12.

The last is the operation of YY

[9] [0-9] [0-9]|2[0][0-9][0-9]

Allows the user to enter the year 1900-2099.

The regular expression for the last date in the dd/mm/yyyy format is:

^ ([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1]) [-/.] ([1-9]|0[1-9]|1[0-2]) [- / .] (1[9][0-9][0-9]|2[0][0-9][0-9]) $
Date in MM/DD/YYYY format:

^ ([1-9]|0[1-9]|1[0-2]) [-/.] ([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1]) [- / .] (1[9][0-9][0-9]|2[0][0-9][0-9]) $
Date in YYYY/MM/DD format:

^ (1[9][0-9][0-9]|2[0][0-9][0-9]) [-/.] ([1-9]|0[1-9]|1[0-2]) [- / .] ([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1]) $


Regular expressions

"^\d+$"//non-negative Integer (positive integer + 0)

"^[0-9]*[1-9][0-9]*$"//Positive integer

"^ ((-\d+) | (0+)) $ "//non-positive integer (negative integer + 0)

"^-[0-9]*[1-9][0-9]*$"//Negative integer

"^-?\d+$"//Integer

"^\d+ (\.\d+)? $"//nonnegative floating-point number (positive float + 0)

"^ ([0-9]+\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*)] $"

Positive floating-point numbers

"^ ((-\d+ (\.\d+)?) | (0+ (\.0+)) $ "//non-positive floating-point number (negative floating-point number + 0)

^ (-([0-9]+\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*))] $"

Negative floating-point numbers

"^ (-?\d+) (\.\d+)? $"//floating-point number

"^[a-za-z]+$"//A string of 26 English letters

"^[a-z]+$"//A string of 26 uppercase letters

"^[a-z]+$"///a string consisting of 26 lowercase letters

"^[a-za-z0-9]+$"//A string of numbers and 26 English letters

"^\w+$"//A string of numbers, 26 English letters, or underscores

"^[\w-]+ (\.[ \w-]+) *@[\w-]+ (\.[ \w-]+) +$ "//email address

"^[a-za-z]+://(\w+ (-\w+) *) (\. ( \w+ (-\w+) *)) * (\?\s*) $ "//url

"^[a-za-z0-9_]*$"


Cases

The following example creates an object (re) that contains a regular expression pattern and associated flags to show you the use of the regular Expression object. In this case, the regular expression object that is the result is also used in the match method:

The code is as follows Copy Code

function Matchdemo ()

{

var r, re; Declare a variable.

var s = "The rain in Spain falls mainly in the plain";

Re = new RegExp ("Ain", "G"); Creates a regular expression object.

r = S.match (re); Finds a match in the string s.

return (R);

}

Some common examples

The code is as follows Copy Code

The checksum is all made up of numbers

function IsDigit (s)

{

var patrn=/^[0-9]{1,20}$/;

if (!patrn.exec (s)) return false

return True

}

Verify Login Name: Can only enter 5-20 letters beginning with a letter, can be with numbers, "_", "." The string

function Isregisterusername (s)

{

var patrn=/^[a-za-z]{1} ([a-za-z0-9]|[. _]) {4,19}$/;

if (!patrn.exec (s)) return false

return True

}

Verify user name: You can enter only 1-30 strings that start with a letter

function Istruename (s)

{

var patrn=/^[a-za-z]{1,30}$/;

if (!patrn.exec (s)) return false

return True

}

}}

Verify password: Can only enter 6-20 letters, numbers, underscores

function ispasswd (s)

{

var patrn=/^ (W) {6,20}$/;

if (!patrn.exec (s)) return false

return True

}

Check ordinary telephone, fax number: You can "+" start, in addition to numbers, can contain "-"

function Istel (s)

{

var patrn=/^[+]{0,1} (d) {1,3}[]? ([-]? (d) {1,12}) +$/;

var patrn=/^[+]{0,1} (d) {1,3}[]? ([-]? ((d) | []) {1,12}) +$/;

if (!patrn.exec (s)) return false

return True

}

Check mobile phone Number: Must start with a number, in addition to the number, can contain "-"

function Ismobil (s)

{

var patrn=/^[+]{0,1} (d) {1,3}[]? ([-]? ((d) | []) {1,12}) +$/;

if (!patrn.exec (s)) return false

return True

}

Check ZIP code

function Ispostalcode (s)

{

var patrn=/^[a-za-z0-9]{3,12}$/;

var patrn=/^[a-za-z0-9]{3,12}$/;

if (!patrn.exec (s)) return false

return True

}

Verify Search Keywords

function Issearch (s)

{

var patrn=/^[^ ' ~!@#$%^&* () +=|\][]{}:; ',. <>/?] {1} [^ ' ~!@$%^& () +=|\]

[]{}:; ',. <>?] {0,19}$/;

if (!patrn.exec (s)) return false

return True

}

function IsIP (s)//by zergling

{

var patrn=/^[0-9.] {1,20}$/;

if (!patrn.exec (s)) return false

return True

}

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.