Javascript: Regular Expression

Source: Internet
Author: User

Regular Expressions are often used in Javascript, such as verifying whether input formats such as email addresses and birthdays are valid. The following describes the regular expression usage in JS:

I. metacharacters

. Match any character, except for line breaks

\ D Number

\ W digits or letters

\ S space, including space, tab, line break, etc.

^ The string starts with the pattern, that is, the matching pattern must start with the string

$ The string ends in the pattern, that is, the matched pattern must be at the end of the string

 

Ii. Qualifier

* Mode appears 0 or multiple times

+ Mode appears once or multiple times

? Mode appears 0 or 1 time

The {n} mode must contain N

A | B. Select one for both.

[ABC] character combination, one of which can be used

() Submode

 

Iii. Regular Expressions

Regular Expressions start and end with/, in the form of/expression/. expression is an expression consisting of the preceding characters (or metacharacters) and delimiters.

VaR RegEx =/^/d {5} $/

If (! RegEx. Test (inputfield. Value ))

// The inputfield value is invalid

Use the test method to determine whether the mode matches

The following is a common regular expression:

1. Set the date format to mm/DD/YYYY or mm/DD/yy.

The corresponding regular expression is:/^ \ D {2} \/\ D {2} \/(\ D {2} | \ D {4}) $/

Note: The/In the date must be escaped \/

2. Email

The general email format is: a string of letters or numbers @ a string of letters or numbers. A string of letters or numbers (length is 2 or 3), such as test111@163.com, 112117@qq.com

The corresponding regular expression is/^ \ W + @ \ W + \. \ W {2, 3} $/

Also consider the need to yo other mail formats, such as: aa-bb@youtube.com.nz, love_you@rock.ca

More general Regular Expressions:/^ [\ W \. -_ \ +] + @ [\ W-] + (\. \ W {2, 4}) + $/(from JavaScript head first)

Related Article

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.