Regular expressions commonly used in web development

Source: Internet
Author: User
Tags preg string methods

In computer science, a regular expression is used to describe or match a series of strings that conform to a certain syntactic rule. In web development, regular expressions are often used to detect and find replacements for certain strings that conform to rules, such as detecting the correctness of user input E-mai format, collecting page content that conforms to rules, and so on.

Today we use PHP and javscript to introduce to you the most commonly used in web development of the most useful regular expressions and their usage, the regular expression is a discipline, it is impossible to use an article to explain, the theory of things online a lot, interested students can search a bunch. But you probably don't have to. Learn elusive regular expressions, see this article and examples to show you a common, useful regular expressions.

PHP Common Expressions Usage

1. Matching positive integers:/^[1-9]\d*$/

2. Match non-negative integers (positive integer +0):/^\d+$/

3. Match Chinese:/^[\x{4e00}-\x{9fa5}]+$/u

4. Match email:/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) */

5. Matching URL URL: (((F|HT) {1} (TP|TPS)://) [[Email protected]:%_\+.~#?&//=]+]

6. Match letter start, 5-16 characters, alphanumeric underline:/^[a-za-z][a-za-z0-9_]{4,15}$/

7. Match numbers, letters, underscores, Chinese:/^[\x{4e00}-\x{9fa5}a-za-z0-9_]+$/u

8. Matching China postcode:/^[1-9]\d{5}$/

9. Match IP address:/\b (?:(? : 25[0-5]|2[0-4][0-9]| [01]? [0-9] [0-9]?) \.) {3} (?: 25[0-5]|2[0-4][0-9]| [01]? [0-9] [0-9]?) \b/

10. Match the Chinese mainland identity card:/^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3} (\d|x| X) $/

Examples of PHP Regular validation string methods:

$str = "Chinese ah"$preg//if(preg_match($preg, $str,$arr)) {      $msg = ' Match succeeded! ';} Else {      $msg = ' match failed! 'echo$msg;
JavaScript Common expression Usage

1. Matching positive integers:/^[0-9]*[1-9][0-9]*$/

2. Match non-negative integers (positive integer +0):/^\d+$/

3. Match Chinese:/^[\u4e00-\u9fa5]/

4. Match email:/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) */

5. Match URL url:/^ (f|ht) {1} (TP|TPS): \/\/([\w-]+\.) +[\w-]+ (\/[\w-./?%&=]*)?

6. Match letter start, 5-16 characters, alphanumeric underline:/^[a-za-z][a-za-z0-9_]{4,15}$/

7. Match numbers, letters, underscores, Chinese:/^[\u4e00-\u9fa5a-za-z0-9_]+$/

8. Matching China postcode:/^[1-9]\d{5}$/

9. Match IP address:/\b (?:(? : 25[0-5]|2[0-4][0-9]| [01]? [0-9] [0-9]?) \.) {3} (?: 25[0-5]|2[0-4][0-9]| [01]? [0-9] [0-9]?) \b/

10. Match the Chinese mainland identity card:/^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3} (\d|x| X) $/

Examples of JavaScript Regular validation string methods:

1 varstr = "[Email protected]"; 2 varPreg =/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) */;//Match Email3 if(Preg.Test (str)) { 4     varmsg = "Match succeeded"; 5}Else{ 6     varmsg = "Match failed! "; 7 } 8Alert (msg);

Regular expressions commonly used in web development

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.