Basic Introduction to Regular expressions

Source: Internet
Author: User

The basic syntax of regular expressions

1. Two special symbols ' ^ ' and ' $ '. Their role is to indicate the beginning and end of a string, respectively.

2. Other ' * ', ' + ', '? ' These three symbols represent the number of occurrences of one or a sequence of characters

The "ab{2}"---indicates that a string has an A followed by 2 B ("ABB");

"Ab{2,}"---indicates that a string has an a followed by at least 2 B;

The "ab{3,5}"---indicates that a string has an a followed by 3 to 5 B;

"ab*" = = ab{0,}

"ab+" = = Ab{1,}

"ab?" = = ab{0,1}

Note that you must specify the lower bound of the range, such as {0,2}, instead of {, 2};

3. There is also a "| representation" or "action

"Hi¦hello": Indicates a string with "hi" or "hello";
"(B¦CD) EF": denotes "bef" or "cdef";
"(a¦b) *c": Represents a String "a" "B" mixed strings followed by a "C";

4. "." can replace any character

"A.[0-9]": Indicates that a string has an a followed by an arbitrary character and a number

"^.3$": Represents a string of any three characters with a length of 3 characters s

5. Regular expression validation controls the input character type of the text box
1. Enter only numbers and English:
<input onkeyup= "Value=value.replace (/[\w]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "id=" Text1 "name=" Text1 ">

2. Only numbers can be entered:
<input onkeyup= "Value=value.replace (/[^\d]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "id=" Text2 "name=" Text2 ">

3. only full-width input:
<input onkeyup= "Value=value.replace (/[^\uff00-\uffff]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\uff00-\uffff]/g, ') "id=" Text3 "name=" Text3 ">

4. Only the Chinese characters can be entered:
<input onkeyup= "Value=value.replace (/[^\u4e00-\u9fa5]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5]/g, ') "id=" Text4 "name=" TEXT4 ">

6. Application examples of regular expressions popular description

Whether the tests are all made up of numbers

:/^[0-9]{1,20}$/

Over here

^ indicates that the beginning character matches the rule immediately following the ^

$ indicates the first character to match the rule immediately preceding the $

The content in [] is an optional character set

[0-9] indicates a range of characters is required between 0-9

{1,20} indicates that the numeric string length is valid from 1 to 20, that is, the number of occurrences of the character in [0-9] is 1 to 20 times

/^ and $/should be used to represent rules that require an exact match of the entire string, rather than just a substring in the string;

****************************************************************

Check login name: Only 5-20 can be entered with a letter beginning with a number, underscore and '. ' The string

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

Here, It is required to be composed of uppercase and lowercase letters, numbers, or special character sets [. _]

******************************************************************

Verify user name: Only 1-30 strings beginning with a letter can be entered

/^[a-za-z]{1,30}$/

*******************************************************************

Check password: Only 6-20 letters, numbers, underscores can be entered

/^ (\w) {6-20}$/

Here, \w: used to match letters, numbers, or underscore characters

*******************************************************************

Verify the regular phone, fax number: can be "+" or the beginning of the number, can contain "-" and ""

/^[+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +$/

\d: Used to match numbers from 0 to 9;

“?” Metacharacters stipulate that their leading objects must appear consecutively 0 or one time in the target object

Can match the string such as: +123-999 999; +123-999 999; 123 999 999; +123 999999 etc.

Basic Introduction to 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.