Regular Expression for matching numbers in Javascript

Source: Internet
Author: User

Usage:
Example:
/** Positive integer matching expression */
VaR pattern =/^ [0-9] * [1-9] [0-9] * $ /;
VaR flag = pattern. Test ();
If a is a positive integer, the Boolean value of flag is true. If a is not a positive integer, the Boolean value of flag is false.

/** Floating point match expression */
VaR pattern =/^ ([-]) {0, 1} ([0-9]) {1,} ([.]) {0, 1} ([0-9]) {0,} $ /;

/** Floating point match expression with only two decimal places reserved */
VaR pattern =/^ -? \ D + [\. \ D]? \ D {0, 2} $ /;

/** Regular floating-point match expression */
VaR pattern =/^ ([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ /;

/** Positive floating point number and retain two decimal match expressions */
VaR pattern =/^ ([1-9] \ D * (\. \ D? [0-9])?) | (0 \. [1-9] [0-9]) | (0 \. [0] [1-9]) $ /;

/** Match expression of negative Floating Point Number */
VaR pattern =/^ (-([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ /;

/** Negative floating point number and retain two decimal match expressions */
VaR pattern =/^-([1-9] \ D * (\. \ D? [0-9])?) | (0 \. [1-9] [0-9]) | (0 \. [0] [1-9]) $ /;

/** Negative floating point number + 0 matching expression */
VaR pattern =/^ (-\ D + (\. \ D + )?) | (0 + (\. 0 + )?)) $ /;

/** Negative floating point number + 0 and retain two decimal match expressions */
VaR pattern =/^ (-\ D + [\. \ D]? \ D {0, 2}) | (0 + (\. 0 + )?)) $ /;

Positive floating point + 0 matching expression
VaR pattern =/^ \ D + (\. \ D + )? $ /;

/** Positive floating point + 0 matching expression with only two decimal places reserved */
VaR pattern =/^ \ D + [\. \ D]? \ D {0, 2} $ /;

/** Integer matching expression */
VaR pattern =/^ -? \ D + $ /;

/** Positive integer matching expression */
VaR pattern =/^ [0-9] * [1-9] [0-9] * $ /;

/** Negative integer matching expression */
VaR pattern =/^-[0-9] * [1-9] [0-9] * $ /;

/** Positive integer + 0 matching expression */
VaR pattern =/^ \ D + $ /;

/** Match expression of negative integer + 0 */
VaR pattern =/^ (-\ D +) | (0 +) $ /;

/** All are digit matching expressions */
VaR pattern =/^ [0-9] {1, 20} $/

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.