Common JavaScript Regular Expression Code sorting (1/4)

Source: Internet
Author: User

I want to write a common JavaScript Regular Expression Code for a friend. I have sorted out some of the following code for reference.


Match the ending number
Source: how to use a javascript Regular Expression to retrieve the last number of strings? Thank you.

For example
30CAC0040 fetch 40
3SFASDF92 fetch 92

The regular expression is as follows:/d + $/g uniform number of spaces
Source: Regular Expression matching space

Character keys in a string contain spaces, but the number of spaces may be inconsistent. The number of spaces is changed to one by regular expression.

Example: Blue

Turn to: Blue

Aobert's regular expression:

The Code is as follows: Copy code
<Script type = "text/javascript">
Var str = "Blue rational thinking"
Var reg =/s +/g
Str = str. replace (reg ,"")
Document. write (str)
</Script>

Determines whether a string is composed of digits.
Source: Is there a simple way to judge whether a string is composed of digits?

This regular expression is relatively simple. I wrote a test

 

The Code is as follows: Copy code
<Script type = "text/javascript">
Function isDigit (str ){
Var reg =/^ d * $ /;
Return reg. test (str );
}
Var str = "7654321 ";
Document. write (isDigit (str ));
Var str = "test ";
Document. write (isDigit (str ));
</Script>

Regular phone number
Source: I want to know the regular expression of the phone number.

: Verify the JS Regular Expression of the phone number.

/^ D {3, 4}-d {7, 8} (-d {3, 4 })? $/The area code must be a number of 3-4 digits. After the area code, use "-" to connect to the phone number.

^ D {3, 4}-7-8 digits

D {} extension number is a 3-4 digit number, which is not required. If yes, it is connected to the phone number "-".

(-D {3, 4 })? Mobile phone number Regular Expression
The mobile phone number for regular expression verification. The preceding 0 value is ignored. The value range is 130-139,150-159. Ignore the first 0 and then judge that it is 11 bits.

Cloeft regular:

/^ 0*(13 | 15) d {9} $/^ 0 * matches any number of 0 at the beginning.

Because the mobile phone number is 13 digits 9 digits, and 15 digits 9 digits, you can use (13 | 15) d {9} to match.

The test code is as follows:

The Code is as follows: Copy code
Function testReg (reg, str ){
Return reg. test (str );
}
Var reg =/^ 0*(13 | 15) d {9} $ /;
Var str = '20140901 ';
Var str2 = '20140901 ';
Var str3 = '20140901 ';
Document. write (testReg (reg, str) + '<br/> ');
Document. write (testReg (reg, str2) + '<br/> ');
Document. write (testReg (reg, str3) + '<br/> ');

Use a regular expression to delete spaces in a string:
Source: Is there any function in js that removes spaces?

The code and test code are as follows:

 

The Code is as follows: Copy code
<Script type = "text/javascript">
// Delete the white spaces on both sides of the string.
Function trim (str ){
Return str. replace (/^ s + | s + $/g ,'');
}
// Delete the white spaces on the left of the string.
Function ltrim (str ){
Return str. replace (/^ s +/g ,'');
}
// Delete the white space characters on the right of the string.
Function rtrim (str ){
Return str. replace (/s + $/g ,'');
}
// The following is the test code.
Var trimTest = "123456789 ";
// There is a space between the front and back.
Document. write ('length: '+ trimTest. length +' <br/> ');
// Before use
Document. write ('ltrim length: '+ ltrim (trimTest). length +' <br/> ');
// After ltrim is used
Document. write ('rtrim length: '+ rtrim (trimTest). length +' <br/> ');
// After rtrim is used
Document. write ('trim length: '+ trim (trimTest). length +' <br/> ');
// After trim is used
</Script>

The test results are as follows:

Length: 11
Ltrim length: 10
Rtrim length: 10
Trim length: 9 Limit text box can only enter numbers, decimal points, and so on
Source: text box input restrictions question ????

Only numbers and decimal places can be entered

Var reg =/^ d *.? D {0, 2} $/starts with a number. There are 0 or a decimal point in the middle, and 0 to 2 digits at the end.

Only lowercase English letters and decimal points can be entered, and colons, forward and backward slashes (:./)

Var reg =/[a-z. /\:] +/; a-z contains lowercase letters ,. it is the decimal point, the backslash (/) and the backslash (\) respectively, and the colon. The entire character set and code can be any one, followed by +, 1 or more.

Replace the content before the decimal point with the specified content
Source: returns a regular expression!

1 2 3 4

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.