JavaScript common Regular Expression collection 1th/2 page _ Regular expression

Source: Internet
Author: User
Tags lowercase uppercase letter
The checksum is all made up of numbers
Copy Code code as follows:

function IsDigit (s)
{
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec (s)) return false
return True
}

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

function Isregisterusername (s)
{
var patrn=/^[a-za-z]{1} ([a-za-z0-9]|[. _]) {4,19}$/;
if (!patrn.exec (s)) return false
return True
}

Verify user name: You can enter only 1-30 strings that start with a letter
Java code
Copy Code code as follows:

function Istruename (s)
{
var patrn=/^[a-za-z]{1,30}$/;
if (!patrn.exec (s)) return false
return True
}

Verify password: Can only enter 6-20 letters, numbers, underscores
Copy Code code as follows:

function ispasswd (s)
{
var patrn=/^ (\w) {6,20}$/;
if (!patrn.exec (s)) return false
return True
}

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

function Istel (s)
{
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? (\d) {1,12}) +$/;
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +$/;
if (!patrn.exec (s)) return false
return True
}

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

function Ismobil (s)
{
var patrn=/^[+]{0,1} (\d) {1,3,5}[]? ([-]? ((\d) | []) {1,12}) +$/;
if (!patrn.exec (s)) return false
return True
}

Check ZIP code
Copy Code code as follows:

function Ispostalcode (s)
{
var patrn=/^[a-za-z0-9]{3,12}$/;
var patrn=/^[a-za-z0-9]{3,12}$/;
if (!patrn.exec (s)) return false
return True
}

Verify Search Keywords
Copy Code code as follows:

function Issearch (s)
{
var patrn=/^[^ ' ~!@#$%^&* () +=|\\\][\]\{\}:; ' \,.<>/?] {1} [^ ' ~!@$%^& () +=|\\\]
[\]\{\}:;' \,.<>?] {0,19}$/;
if (!patrn.exec (s)) return false
return True
}

Verifying IP
Copy Code code as follows:

function IsIP (s)//by zergling
{
var patrn=/^[0-9.] {1,20}$/;
if (!patrn.exec (s)) return false
return True
}

Regular expressions
"^\\d+$"//non-negative 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+)? $"//nonnegative floating-point number (positive float + 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 numbers
"^ ((-\\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 numbers
"^ (-?\\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 of numbers and 26 English letters
"^\\w+$"//A string of numbers, 26 English letters, or underscores
"^[\\w-]+ (\\.[ \\w-]+) *@[\\w-]+ (\\.[ \\w-]+) +$ "//email address
"^[a-za-z]+://(\\w+ (-\\w+) *) (\ \\w+ (-\\w+) *)) * (\\?\\s*) $ "//url
"^[a-za-z0-9_]*$"
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.
\s: Used to match a single spaces, including tab keys and line breaks;
\s: Used to match all characters except a single spaces;
\d: Used to match numbers from 0 to 9;
\w: Used to match letters, numbers, or underscore characters;
\w: Used to match all characters that do not match the \w;
: Used to match all characters except for line breaks. (Note: We can think of \s and \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. The more commonly used locator characters include: "^", "$", "\b" and "\b".
The "^" locator stipulates that the match pattern must be present at the beginning of the target string
The "$" locator stipulates that the matching pattern must be present at the end of the target object
The "\b" locator stipulates that the matching pattern must now be one of the two boundaries at the beginning or end of the target string
The "\b" locator stipulates that the matching object must be within two boundaries at the beginning and end of the target string, that is, the matching object can neither be the beginning of the target string nor the end of the target string.
Similarly, we can think of "^" and "$" and "\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 regular expression pattern above 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 the 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:
/[a-z]/the above regular expression will match any uppercase letter from a to Z range.
/[a-z]/the above regular expression will match any lowercase letter from a to Z range.
/[0-9]/the above regular expressions will match any number in the range 0 through 9.
/([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 "\" is 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:
1. \ escape Character
2. (), (?:), (? =), [] parentheses and square brackets
3. *, +,?, {n}, {n,}, {n,m} qualifier
4. ^, $, \anymetacharacter position and order
5.| " or the action
Working with instances
In JavaScript. 1.2 has a powerful regexp () object that can be used to match 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.
Regular Expression objects This object contains regular expression patterns and flags that indicate how patterns are applied.
Syntax 1 re =/pattern/[flags]
Syntax 2 re = new RegExp ("pattern", ["flags"])
Parameters
Re
Required option. The name of the variable that will be assigned the regular expression pattern.
Pattern
Required option. The regular expression pattern to use. If Syntax 1 is used, the pattern is separated by the "/" character. If Syntax 2, enclose the pattern in quotation marks.
Flags
Options available. If you use Syntax 2, enclose the flag in quotation marks. Flags can be used in combination, available in:
G (all occurrences of pattern in full text search)
I (Ignore case)
m (Multiple-line lookup)
Example
The following example creates an object (re) that contains a regular expression pattern and associated flags to show you the use of the regular Expression object. In this case, the regular expression object that is the result is also used in the match method:
function Matchdemo ()
{
var r, re; Declare a variable.
var s = "The rain in Spain falls mainly in the plain";
Re = new RegExp ("Ain", "G"); Creates a regular expression object.
r = S.match (re); Finds a match in the string s.
return (R);
}
return value: ain,ain,ain,ain\\
Property Lastindex Properties | Source property \ \
Method Compile Method | exec Method | Test method \
Request Version 3\\
See RegExp Objects | Regular expression Syntax | String object \ \
exec method
Runs a lookup in a string with a regular expression pattern and returns an array that contains the result of the lookup.
Rgexp.exec (str)
Parameters
Rgexp
Required option. A regular expression object that contains the regular expression pattern and the available flags.
Str
Required option. The string object or string literal in which to perform the lookup.
Description \
If the Exec method does not find a match, it returns NULL. If it finds a match, the Exec method returns an array and updates the properties of the global RegExp object to reflect the result of the match. The 0 elements of the array contain a complete match, and the 1th to n element contains any of the child matches that appear in the match. This is equivalent to no match method for setting global flag (g).
If the global flag is set for a regular expression, exec starts the lookup from the position indicated by the value of lastindex. If the global flag is not set, EXEC ignores the lastindex value and starts the search from the beginning of the string.
The Exec method returns an array that has three properties, namely input, index, and lastindex. The Input property contains the entire string being looked up. The Index property contains the position of the entire substring found to be matched in the lookup string. The Lastindex property contains the next position of the last character in the match.
Example
The following example illustrates the use of the Exec method:
Copy Code code as follows:

function Regexptest ()
{
var ver = number (ScriptEngineMajorVersion () + "." + ScriptEngineMinorVersion ())
if (ver >= 5.5) {//test the version of JScript.
var src = "The rain in Spain falls mainly the plain."
var re =/\w+/g; Creates a regular expression pattern.
var arr;
while ((arr = re.exec (src))!= null)
document.write (Arr.index + "-" + Arr.lastindex + arr + "T");
}
else{
Alert ("Please use JScript.") Updated version ");
}
}

return value: 0-3the 4-8rain 9-11in 12-17spain 18-23falls 24-30mainly 31-33in 34-37the

Current 1/2 page 12 Next read the full text
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.