Regular Expression Basics

Source: Internet
Author: User

Regular expression: Regular expressions are also known as regular expressions

A concept of computer science. A regular expression uses a single string to describe and match a series of strings that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern.

Tips Before you learn
Line feed ' \ n ' and carriage return ' \ R '

As the name implies, the line break is another line, the carriage return is the beginning of the line back, so we usually write a file carriage return should be exactly called carriage return newline character

' \ n ' 10 line break (newline)
' \ r ' 13 return Vehicle (return)


Can also be expressed as ' \x0a ' and ' \x0d '. (16 binary)

Under Windows System, the carriage return newline symbol is "\ r \ n". But there is no "\ r" symbol in Linux and other systems.

In parsing text or other format of the file content, often encounter the decision to enter the line of the place, this time should pay attention to both "\ r \ n" and also determine "\ n".

You can get a line when you write a program, trim it off ' \ R ', and get the string you need.

Regular expression meta-characters

<table border="1"cellpadding="0"> <tr> <th> Symbols </th> <th> description </th> </tr> &lt            ;tr> <td>^</td> <td> start of matching string </td> </tr> <tr> <td>$</td> <td> match end of string </td> </tr> <tr> & lt;td>\s</td> <td> any blank string </td> </tr> <tr> <td>\ S</td> <td> Miami non-blank string </td> </tr> <tr> <td>\d</td&            Gt <td> matches a numeric character, equivalent to [0-9]</td> </tr> <tr> <td>\D</td> <td> matches any character other than a numeric character, equivalent in [^0-9]</td> </tr> <tr> <td>\w</td> <td> match a number, underscore, or alphabetic character, equivalent to [a-za-z_0-9]</td> </tr> <tr> <td>\W</td> <td> matches any non-single character, equivalent to [^a -za-z_0-9]</td> </tr> <tr> <td>.</td> <td> Any character other than line break </ Td> </tr> </table>

Note: In regular expressions, we use casing to represent the inverse
This ^ symbol in the table indicates "non"

General use \w to match user name, mailbox name
\s is generally used to match any non-empty
Generally used. to match any character

Example

Email: [Email protected]
qq:234534324
E-mail: ^\[email protected]\w.[a-za-z]$
qq:\d{5,12} {} indicates the number of occurrences, here is 5 to 12 times

The above notation is wrong! Because only a single character is matched!!

Match symbol

<table border="1"> <tr> <th> Match symbol </th> <th> description </th> </tr> & Lt;tr> <td>{n}</td> <td> matches the previous item n times </td> </tr> <tr&gt            ; <td>{n,}</td> <td> matches the previous item n times, or multiple </td> </tr> <tr> &L T;td>{n,m}</td> <td> matches the previous item n times, but not more than m times </td> </tr> <tr> & Lt;td>*</td> <td> matches the previous item 0 or more times, equivalent to {0,}</td> </tr> <tr> <td>+</td> <td> matches the previous item 1 or more times, equivalent to {1,0}</td> </tr> <tr> <td>?</td> <td> matches the previous item 0 or 1 times, equivalent to {0,1}</td> </tr> </table>

So:

email [email protected] (. cn)

The correct regular expression is: ^\[email protected]\w+\. [A-za-z] {2-3} (\. [A-za-z] {2-3}?)

Note: The match symbol matches the single character in front of it
To match any character other than \s\n (newline character), so this is done with \ Escape


Regularexpresssion Regular Expression (REGEXP)

Grammar
var reg1=new RegExp (/^\w{2,3}/);
var reg2=new RegExp ("Red", "I") (G-Global find M-multiline lookup)
Shorthand
var reg1=/^\w{2,3}/;
var reg2=/red/i;

Note: The first method inside the parameter is our regular expression, the second method inside the parameter is a normal string.

Example

var regqq=/^\d{5,12}$/;
var strqq= "55"


var result=regqq.test (STRQQ);
Note Test (): Verifies that the target string conforms to the regular expression rule, and the return value is a Boolean type


Methods for string objects to support regular expressions

Search (Regular expression): Returns the location of the match
Match (regular expression): Returns a matching string
Replace (regular expression, to replace string): Returns the replaced result

Note: This three method is not a regular expression method, but a method of the string object

Regular Expression Basics

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.