Regular expressions in JavaScript

Source: Internet
Author: User

Some of the functions of regular expressions:
1. Test a pattern for a string. For example, you can test an input string to see if there is a phone number pattern or a credit card number pattern in the string. This is called data validation
2, replace the text. You can use a regular expression in your document to identify specific text, and then you can either delete it all or replace it with another text
3. Extracts a substring from a string based on pattern matching. Can be used to find specific text in text or input fields

17 Regular Expressions for javascript:
"^\\d+$"//nonnegative integer (positive integer + 0)
"^[0-9]*[1-9][0-9]*$"//Positive integer
"^ ((-\\d+) | (0+)) $ "//non-positive integer (negative integer + 0)
"^-[0-9]*[1-9][0-9]*$"//Negative integer
"^-?\\d+$"//Integer
"^\\d+ (\\.\\d+)? $"//non-negative floating-point number (positive floating point + 0)
^ ([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
"^ ((-\\d+ (\\.\\d+)?) | (0+ (\\.0+)?)) $ "//non-positive floating-point number (negative floating-point number + 0)
^ (-([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
^ (-?\\d+) (\\.\\d+)? $ "//floating-point number
"^[a-za-z]+$"//A string consisting of 26 English letters
"^[a-z]+$"//A string consisting of 26 uppercase letters in English
"^[a-z]+$"//String consisting of 26 English letters in lowercase
"^[a-za-z0-9]+$"//string consisting of a number and 26 English letters
"^\\w+$"//A string consisting of numbers, 26 letters or underscores
"^[\\w-]+ (\\.[ \\w-]+) *@[\\w-]+ (\\.[ \\w-]+) +$ "//email address
"^[a-za-z]+://(\\w+ (-\\w+) *) ( \\w+ (-\\w+) *) * (\\?\\s*)? $ "//url

A few small examples of simple information validation using regular expressions in javascript:

1, using the JS regular expression to determine whether it is 0-9 of the Arabic numerals

function Regisdigit (fData) {    var reg = new RegExp ("^[0-9]$");    Return (Reg.test (FData));}

2, using the JS regular expression to get the length of the string

function Regdatalength (fData) {    var vallength = fdata.length;    var reg = new RegExp ("^[\u0391-\uffe5]$");    var result = 0;    for (i=0; i< vallength; i++)    {        if (reg.test (Fdata.charat (i)))        {            result + = 2;        }        else        {            result + +;        }    }    return result;}

3, JS judge whether the value

function Regisnumber (fData) {    var reg = new RegExp ("^[-]?[ 0-9]+[\.]? [0-9]+$ ");    Return Reg.test (FData)}

4, JS verify email is correct

function Regisnumber (fData) {    var reg = new RegExp ("^[-]?[ 0-9]+[\.]? [0-9]+$ ");    Return Reg.test (FData)}

5, JS to determine whether the phone number is correct

function Regisphone (fData)   {       var reg =/^ (\+86)? ( 1[0-9]{10}) $/;       Return Reg.test (FData);   }


Note: The above content from my online simple collation


Regular expressions in JavaScript

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.