Regular expressions detailed _ regular expressions

Source: Internet
Author: User
Tags html tags

Regular expressions, also known as formal representations, general representations (English: Regular Expression, often abbreviated as regex, RegExp, or re) in code, 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.

What can regular expressions do?

1, data validation, you can test a string to see if the string conforms to a certain rule.

2, replace text, you can delete the specified text in the document, or replace with other text.

3. Extracts a substring from the string to find specific text in the text or input field.

Regular expression syntax

A regular expression is a literal pattern of ordinary characters (A-Z) and special characters (metacharacters) that, as a template, match a character pattern to the string being searched.

Metacharacters

Private characters that have special meaning.

Basic Meta characters

. Matches any character other than a newline, such as a regular expression r.t can match "rat, rut, R T", but cannot match root

[] matches a character that appears in []

| or sensitive words AB|CD|ED|DF

() Increase priority A (BC) implementation groupings

Qualifying Meta characters

* Repeat 0 or more times

+ Repeat one or more times

? Repeat 0 times or once

{n} repeat n times

{N,} repeat n or more times

{N,m} repeats n to M times

Unary characters

^ Match string start such as regular expression ^when can match to the start of "when in", but cannot match "what and" in "

$ match string end such as regular expression food$ can match to the end of "he's food"

Abbreviated form

\b The beginning or end of a word

\d Matching 0-9 Digital \d non-numeric

\s any whitespace character including tab and newline \s all characters other than uppercase and white

\w matches letters, numbers, or underscores \w not letters, numbers underline

Escape character

If you want to look up the meta character itself, for example, if you want to find it. or * There will be problems, because they will be interpreted as other meanings. Then you need to use \ To remove the special meaning of these characters.

Therefore, you need to use \. \* find \ itself should write \

Using regular expressions in JavaScript

Creating regular Expressions

Literal value
var reg =/\d+/;
Constructor
var regx = new RegExp (\\d+);

The

if (Reg.test ("45646515"))
{
  alert ("OK");
}

if (Regx.test ("SD"))
{
  alert ("OK")
}

Extraction

var str= "853020304@qq.com";
var reg =/([a-za-z0-9]+) @ ([a-za-z0-9]+ (\.[ a-za-z0-9]+) +)/;

var match = reg.exec (str);
Alert (match[0]); 853020304@qq.com match to the object
alert (match[1]);//853020304 [1][2] are each group
alert (match[2);//qq.com
Alert (Match[3]); . com

Circular extraction

var str = "12345";
var r =/\d\d/g; G Global Match
var m = r.exec (str);//12
alert (m);
m = r.exec (str);
alert (m);
m = r.exec (str); NULL does not return null
alert (m);

var arr = [];
var m = null;
while ((M=r.exec (str))!= null)//if not equal to null continuation loop
{
  arr.push (m);//Add to array
}
alert (arr.length);//[0 ]=12 [1]=34

Replace

String str = <string>.replace (regular expression or string, replaced with a string);

var str = "Ab--c--d--e--f-g";
var result = str.replace (/-+/g, ', '); If G is not written, only the first
alert (result) is replaced;//ab,c,d,e,f,g

var date = "August 24, 2015";
var result = Date.replace (\d+) (\d+) month (\d+) day/, "$1-$2-$3");
alert (result);//2015-8-24

Using regular expressions in C #

var regx = "^[a-za-z0-9]{6,20}$";
if (! Regex.IsMatch ("abcdef;sd123", regex)
{
  //length must be 6-20, letters and Numbers
}

Common expressions

Matching ID: \d{15}|\d{18}

Match China ZIP Code: [1-9]\d{5} (?! \d)

Matching Tencent QQ Number: [1-9][0-9]{4,}

Match domestic phone number: \d{3}-\d{8}|\d{4}-\d{7}

Match account number is legal (beginning of letter, allow 5-16 bytes, allow alphanumeric underline): ^[a-za-z][a-za-z0-9_]{4,15}$

Regular expressions that match URL URLs: [a-za-z]+://[^\s]*

Regular expression matching an email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *

A regular expression that matches the end-end whitespace character: ^\s*|\s*$

Regular expression:< matching HTML tags (\s*?) [^>]*>.*?</\1>|<.*? />

Matching regular expressions for Chinese characters: [\U4E00-\U9FA5]

Restrict text box entry in a Web page form

Only Chinese can be entered:

<input type= "text" onkeyup= "Value=value.replace (/[^\u4e00-\u9fa5]/g, ')" Onbeforepaste= "Clipboarddata.setdata ( ' Text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5]/g, ') "/>

Only numbers can be entered:

<input type= "text" onkeyup= "Value=value.replace (/[^\d]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "/>

Only numbers and English can be entered:

<input type= "text" onkeyup= "Value=value.replace (/[\w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text). Replace (/[^\d]/g, ') "/>

The above content is this article to the regular expression does the detailed explanation, hoped everybody likes

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.