JavaScript Regular Expressions-common code collation (1/4)

Source: Internet
Author: User
Tags character set lowercase rtrim


Match the end of the number
Source: How to use JavaScript regular expressions to remove the last set of numbers in a string, thank you.

Such as
30cac0040 Take out 40
3sfasdf92 Take out 92

Is the following:/d+$/g uniform number of spaces
Source: The problem of regular matching spaces

Characters in the string keys have spaces, but the number of spaces may be inconsistent, through the regular number of spaces to unify into one.

For example: Blue ideal

Become: Blue Ideal

The regular of the Aobert:

The code is as follows Copy Code
<script type= "Text/javascript" >
var str= "Blue Ideal"
var reg=/s+/g
str = str.replace (Reg, "")
document.write (str)
</script>

Determine if a string is made up of numbers
Source: Is there a simple way to determine if a string is made up of numbers?

This is relatively simple, write 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>

Phone number is regular
Source: To ask about the regular judgment of the telephone number

: To find a verification phone number JS regular

/^d{3,4}-d{7,8} (-d{3,4})? The $/area code must be filled with a 3-4-digit number, followed by "-" to connect to the phone number

^d{3,4}-number with 7-8 digits

d{7,8} extension number is 3-4 digits, not required, but if filled with "-" and telephone number to connect

(-d{3,4})? cell phone number Regular expression
Verify the mobile phone number, ignore the front 0, support 130-139,150-159. Ignore the front 0 and judge it to be 11-bit.

The regular of the Cloeft:

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

Because the mobile phone number is 13 any number 9 bits, and 15 any number 9 bits, so can use (13|15) d{9} 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 = ' 13889294444 ';
var str2 = ' 12889293333 ';
var str3 = ' 23445567 ';
document.write (Testreg (reg,str) + ' <br/> ');
document.write (Testreg (REG,STR2) + ' <br/> ');
document.write (Testreg (REG,STR3) + ' <br/> ');

To use a regular expression to implement a deletion of a space in a string:
Source: Is there any function to remove the space in JS?

The code and test code are as follows:

  code is as follows copy code
<script type= "Text/javascript" >
Deletes white space characters on either side of the string.
function Trim (str) {
Return Str.replace (/^s+|s+$/g, "");
}
Deletes the white space character to the left of the string.
function LTrim (str) {
Return Str.replace (/^s+/g, "");
}
Deletes the white space character to 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 before and after.
document.write (' Length: ' +trimtest.length+ ' <br/> ');
Before use
document.write (' LTrim length: ' +ltrim (trimtest). length+ ' <br/> ');
After using LTrim
document.write (' RTrim length: ' +rtrim (trimtest). length+ ' <br/> ');
After using RTrim
document.write (' Trim length: ' +trim (trimtest). length+ ' <br/> ');
After using trim
</script>

The results of the test are as follows:

Length:11
LTrim length:10
RTrim Length:10
Trim Length:9 restricted text boxes can only enter numbers and decimal points, and so on
Source: Problem with text box input restrictions????

Only digits and decimal points can be entered

var reg =/^d*.? The d{0,2}$/begins with several numbers, with 0 or a decimal points in the middle, ending with 0 to 2 digits.

You can only enter lowercase English letters and decimal points, and colons, positive and negative slashes (:./)

The var reg =/[a-z./\:]+/;a-z includes lowercase English letters,. is the decimal point,/and \ is the left and right backslash, and finally the colon. The entire composition of a character set and code can be either, and finally in addition to +,1 or more.

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

Home 1 2 3 4 last page
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.