JavaScript regular expression of Xiang solution

Source: Internet
Author: User
Tags lowercase uppercase letter

The use of regular expressions in the detailed
Brief introduction

In short, regular expressions are a powerful tool that can be used for pattern matching and substitution. Its role is as follows:
Tests a pattern of 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 known as data validation.
Replaces text. You can use a regular expression in your document to identify specific text, and then you can delete it all, or replace it with another text.
Extracts a substring from a string based on pattern matching. Can be used to find specific text in text or input fields.

Basic syntax

After a preliminary understanding of the function and function of regular expressions, let's take a look at the syntax format of regular expressions.
Regular expressions are generally as follows:

The part of the/love/where the "/" delimiter is located is the pattern that will match in the target object. The user simply puts the pattern content that wants to find the matching object in between the "/" delimiters. Regular expressions provide specialized "meta characters" to enable users to customize schema content more flexibly. The term "metacharacters" refers to those special characters that have special meaning in regular expressions and can be used to specify the mode in which the leading character (that is, the character at the front of the metacharacters) appears in the target object.
The more commonly used meta characters include: "+", "*", and "?".

The "+" metacharacters specify that its leading characters must appear consecutively or repeatedly in the target object.

The "*" Meta character stipulates that its leading characters must appear 0 or more times in the target object.

“?” Metacharacters specify that its leading object must appear 0 or one consecutive times in the target object.

Next, let's look at the specific application of regular expression meta characters.

/fo+/because the preceding regular expression contains a "+" metacharacters, it means that a string of one or more letters O can be matched with the "fool", "fo", or "football" in the target object after the letter F.

/eg*/because the preceding regular expression contains a "*" metacharacters, the representation can match a string of 0 or more letters G in the target object, such as "Easy", "ego", or "egg", after the letter E.

/wil?/because the above regular expression contains ". A meta character that matches a string of 0 or one letter L that can occur consecutively after the letter I in the target object, such as "Win" or "Wilson".

Sometimes you don't know how many characters to match. To accommodate this uncertainty, regular expressions support the concept of qualifiers. These qualifiers can specify how many times a given component of a regular expression must appear to satisfy a match.

{n} n is a non-negative integer. Matches the determined n times. For example, ' o{2} ' cannot match ' o ' in ' Bob ', but can match two o in ' food '.

{N,} n is a non-negative integer. Match at least n times. For example, ' o{2,} ' cannot match ' o ' in ' Bob ' but can match all o in ' Foooood '. ' O{1,} ' is equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '.

{n,m} m and n are non-negative integers, where n <= m. Matches n times at least and matches up to M times. For example, "o{1,3}" will match the first three o in "Fooooood". ' o{0,1} ' is equivalent to ' o '. Notice that there is no space between the comma and the two number.

In addition to metacharacters, users can specify exactly how often a pattern will appear in a matching object. For example,/jim {2,6}/The regular expression above stipulates that the character M can appear consecutively 2-6 times in a matching object, so the regular expression above can match a string such as Jimmy or Jimmmmmy.
After a preliminary understanding of how to use regular expressions, let's look at how other important metacharacters are used.
Code

1./s: Used to match a single spaces, including tab keys and line breaks;
2./S: Used to match all characters except a single spaces;
3./d: Used to match numbers from 0 to 9;
4./w: Used to match letters, numbers or underscore characters;
5./W: Used to match all characters that do not match/w;
6.. : Used to match all characters except for line breaks.


(Note: We can think of/s as well as/w and/w as inverse)
Below, let's take a look at how to use the above metacharacters in regular expressions.
s+/the above regular expression can be used to match one or more whitespace characters in the target object.
D000/If we have a complex financial statement in hand, then we can easily find all sums up to thousand dollars through the regular expressions mentioned above.
In addition to the meta characters we have described above, there is another unique special character in the regular expression, that is, the locator. The locator character is used to specify where the match pattern appears in the target object. More commonly used locator characters include: "^", "$", "B" and "B".
Code

1. The "^" locator stipulates that the matching pattern must be present at the beginning of the target string
2. The "$" locator stipulates that the matching pattern must be present at the end of the target object
3. The "/b" locator specified that the matching pattern must now be one of the two boundaries at the beginning or end of the target string
4. The "/b" locator stipulates that the matching object must be within two boundaries at the beginning and end of the target string,
5. The matched object cannot be either the beginning of the target string or the end of the target string.


Similarly, we can consider "^" and "$" as well as "/b" and "/b" as two sets of locators that are mutually inverse operations. For example:/^hell/because the above regular expression contains a "^" Locator, you can match a string that starts with "hell", "Hello", or "Hellhound" in the target object. /ar$/because the above regular expression contains a "$" locator, you can match a string that ends with "car", "bar", or "AR" in the target object. bbom/because the above regular expression pattern starts with the "/b" locator, you can match a string that starts with "bomb" or "BOM" in the target object. /man/b/because the above regular expression pattern ends with the "/b" locator, you can match a string that ends with "human", "Woman", or "man" in the target object.
In order to make it easier for users to set a matching pattern, regular expressions allow the user to specify a range in the matching pattern and not be limited to specific characters. For example:
Code

1./[a-z]/The above regular expressions will match any uppercase letter from a to Z range.
2./[a-z]/the above regular expressions will match any lowercase letter from a to Z range.
3./[0-9]/the above regular expressions will match any number in the range from 0 to 9.
4./([a-z][a-z][0-9]) +/the above regular expression will match any string of letters and numbers, such as "aB0".


The point to note here is that you can use "()" to group strings together in regular expressions. The "()" symbol contains content that must also appear in the target object. Therefore, these regular expressions will not match strings such as "ABC", because the last character in "ABC" is a letter rather than a number.
If we want to implement a "or" operation in a regular expression that is similar in programming logic, you can use the pipe character "|" If you choose one of several different modes to match. For example:/to|too|2/the above regular expression will match "to", "too", or "2" in the target object.
There is also a more commonly used operator in the regular expression, that is, the negative character "[^]". Unlike the locator "^" described in the previous article, the negative character "[^]" stipulates that the string specified in the pattern cannot exist in the target object. For example:/[^a-c]/the above string will match any character except A,b, and C in the target object. Generally, when "^" appears inside "[]", it is regarded as a negation operator, and when "^" is outside "[]" or there is no "[]", it should be treated as a locator character.
Finally, the escape character "/" can be used when the user needs to add metacharacters to the pattern of regular expressions and find their matching objects. For example:/th/*/The regular expression above will match the "th*" in the target object rather than the "the".
After a regular expression is constructed, it can be evaluated like a mathematical expression, that is, it can be evaluated from left to right and in order of precedence. Priority is as follows:
Code

1.1. /Escape character
2.2. (), (?, (? =), [] parentheses and square brackets
3.3. *, +,?, {n}, {n,}, {n,m} qualifier
4.4. ^, $,/anymetacharacter position and order
5.5.| " or the action

Working with instances
With a powerful regexp () object in JavaScript 1.2, you can use a matching operation for regular expressions. The test () method can verify that the target object contains a match pattern and returns TRUE or false accordingly.
We can use JavaScript to write the following script to verify the validity of the email address entered by the user.
Code

1. 2. 3. <script language= "Javascript1.2" >
4. <!--start hiding
5. Function verifyaddress (obj)
6. {
7. var email = obj.email.value;
8. var pattern =
9./^ ([a-za-z0-9_-]) +@ ([a-za-z0-9_-]) + (/.[ A-za-z0-9_-]) +/;
Flag = pattern.test (email);
One. if (flag)
12. {
Alert ("Your email address is correct!");
return true;
15.}
. else
17. {
Alert ("Please try again!");
return false;
20.}
21.}
//Stop Hiding-->
</script>
<body>
<form onsubmit= "return verifyaddress (this);" >
<input name= "Email" type= "text" >
<input type= "Submit" >
</form>
</body>

The regular expression of JS

The checksum is all made up of numbers
Code

1. function IsDigit (s)
2. {
3. var patrn=/^[0-9]{1,20}$/;
4. If (!patrn.exec (s)) return false
5. Return True
6.}

Verify Login Name: Can only enter 5-20 letters beginning with a letter, can be with numbers, "_", "." The string
Code

1. function Isregisterusername (s)
2. {
3. Var patrn=/^[a-za-z]{1} ([a-za-z0-9]|[. _]) {4,19}$/;
4. If (!patrn.exec (s)) return false
5. Return True
6.}

Verify user name: You can enter only 1-30 strings that start with a letter
Code

1. function Istruename (s)
2. {
3. var patrn=/^[a-za-z]{1,30}$/;
4. If (!patrn.exec (s)) return false
5. Return True
6.}
7.}}
8.
9.//Check Password: Only 6-20 letters, numbers, underscores can be entered
[Code]
function ispasswd (s)
12. {
var patrn=/^ (/w) {6,20}$/;
if (!patrn.exec (s)) return false
return True
16.}

Check ordinary telephone, fax number: You can "+" start, in addition to numbers, can contain "-"
Code

1. Function Istel (s)
2. {
3.//var patrn=/^[+]{0,1} (/d) {1,3}[]? ([-]? (/d) {1,12}) +$/;
4. Var patrn=/^[+]{0,1} (/d) {1,3}[]? ([-]? ((d) | []) {1,12}) +$/;
5. If (!patrn.exec (s)) return false
6. Return True
7.}

Check mobile phone Number: Must start with a number, in addition to the number, can contain "-"
Code

1. function Ismobil (s)
2. {
3. Var patrn=/^[+]{0,1} (/d) {1,3}[]? ([-]? ((d) | []) {1,12}) +$/;
4. If (!patrn.exec (s)) return false
5. Return True
6.}

Check ZIP code
Code

1. function Ispostalcode (s)
2. {
3.//var patrn=/^[a-za-z0-9]{3,12}$/;
4. var patrn=/^[a-za-z0-9]{3,12}$/;
5. If (!patrn.exec (s)) return false
6. Return True
7.}

Verify Search Keywords
Code

1. function Issearch (s)
2. {
3. Var patrn=/^[^ ' ~!@#$%^&* () +=|///][/]/{/}:; ' /,.<>/?] {1} [^ ' ~!@$%^& () +=|///]
4. [/]/{/}:; ' /,.<>?] {0,19}$/;
5. If (!patrn.exec (s)) return false
6. Return True
7.}
8.
9. Function IsIP (s)//by zergling
10. {
One. var patrn=/^[0-9.] {1,20}$/;
if (!patrn.exec (s)) return false
return True
14.}

Regular expressions
Code

1. "^//d+$"//non-negative Integer (positive integer + 0)
2. "^[0-9]*[1-9][0-9]*$"//Positive integer
3. "^ (-//d+) | ( 0+)) $ "//non-positive integer (negative integer + 0)
4. "^-[0-9]*[1-9][0-9]*$"//Negative integer
5. "^-?//d+$"//Integer
6. "^//d+ (//.//d+)? $ "//nonnegative floating-point number (positive floating-point number + 0)
7. "^ ([0-9]+//.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*//. [0-9]+) | ([0-9]*[1-9][0-9]*)] $"
8.//Positive floating-point number
9. "^ (-//d+
(//.//d+) | (0+ (//.0+)) $ "//non-positive floating-point number (negative floating-point number + 0)
10. "^" (-([0-9]+//.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*//. [0-9]+) | ([0-9]*[1-9][0-9]*))] $"
11.//Negative floating-point number
A. "^ (-?//d+) (
.//d+)? $ "//floating-point number
"^[a-za-z]+$"//A string of 26 English letters
"^[a-z]+$"//A string of 26 uppercase letters
"^[a-z]+$"//A string consisting of 26 lowercase letters
"^[a-za-z0-9]+$"//A string consisting of numbers and 26 English letters
"^//w+$"//A string consisting of a number, 26 English letters, or underscores
A. "^[//w-]+ (
//. [//w-]+) *@[//w-]+ (//.[ w-]+) +$ "//email address
^[a-za-z]+://(
//w+ (-//w+) *) (//. (//w+ (-//w+) *)) * (//?//s*) $ "//url
"^[a-za-z0-9_]*$"

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.