JS common regular expression form validation code

Time:2021-5-4

Use of regular expressions

brief introduction

In short, regular expressions are a powerful tool for pattern matching and replacement. Its functions are as follows:
A pattern of the test 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.
Replace text. You can use a regular expression in the document to identify specific text, and then you can delete all of them, or replace them with other text.
Extract a substring from a string based on pattern matching. It can be used to find specific text in text or input fields.

Basic grammar

After having a preliminary understanding of the functions and functions of regular expressions, let’s take a concrete look at the syntax format of regular expressions.

The form of regular expression is generally as follows:

/The part between the ‘/’ delimiters in love / is the pattern to be matched in the target object. Users only need to put the content of the pattern they want to find the matching object between the ‘/’ delimiters. In order to enable users to customize the content of patterns more flexibly, regular expressions provide special “metacharacters”. Metacharacter refers to those special characters with special meaning in regular expression, which can be used to specify the appearance mode of the leading character (that is, the character in front of metacharacter) in the target object.
The commonly used metacharacters include “+”, “*”, and “?”.
The “+” metacharacter specifies that its leading character must appear one or more consecutive times in the target object.
The “*” metacharacter specifies that its leading character must appear zero or consecutive times in the target object.
“?” Metacharacter specifies that its leading object must appear zero or once in a row in the target object.

Next, let’s take a look at the specific application of regular expression metacharacter.

/Fo + / because the above regular expression contains “+” metacharacter, it means that it can match the string of “football”, “fo”, or “football” in the target object with one or more letters o after the letter F.
/Eg * / because the above regular expression contains the “*” metacharacter, it means that it can match with the string of “easy”, “ego”, or “egg” in the target object with zero or more letters G after the letter E.
/Wil?/ Because the regular expression above contains “?” Metacharacter, which means that it can match the string of “win” or “Wilson” in the target object with zero or one letter L after the letter I.

Sometimes I don’t know how many characters to match. In order to adapt to 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 the match.

{n} N is a nonnegative integer. Match the determined n times. For example, ‘O {2}’ cannot match ‘o’ in ‘Bob’, but it can match two o’s in ‘food’.
{n,} n is a nonnegative integer. Match at least N times. For example, ‘O {2,}’ can’t match ‘o’ in ‘Bob’, but can match all o’s in ‘Foooood’ O {1,} ‘is equivalent to’ O +. ‘ O {0,} is equivalent to o *.
Both {n, m} m and N are non negative integers, where n < = M. The minimum number of matches is n and the maximum number of matches is m. For example, “O {1,3}” will match the first three o’s in “food.” O {0,1} is equivalent to ‘o?’. Note that there cannot be a space between a comma and two numbers.
In addition to metacharacters, users can also specify exactly how often patterns appear in the matching object. For example, / Jim {2,6} / the above regular expression specifies that the character M can appear 2-6 times in a row in the matching object. Therefore, the above regular expression can match with strings such as Jimmy or Jimmy.

Now that we have a good idea of how to use regular expressions, let’s take a look at several other important metacharacters.
\s: It is used to match single space character, including tab key and newline character;
\S: Used to match all characters except single space character;
\d: Used to match numbers from 0 to 9;
\w: Used to match letters, numbers or underscores;
\W: It is used to match all characters that do not match with the character;
.: used to match all characters except newline.

(Note: we can regard the two operations as reciprocal operations.)

Now, let’s see how to use the above metacharacters in regular expressions through examples.

/\S + / the above regular expression can be used to match one or more space characters in the target object.
/\If we have a complicated financial statement in hand, we can easily find all the money with a total amount of 1000 yuan through the above regular expression.
In addition to the metacharacter introduced above, there is another unique special character in regular expressions, namely locator. Locators are used to specify where matching patterns appear in the target object. The commonly used locators include “^”, “$”, “B” and “\ B”.
The “^” locator specifies that the matching pattern must appear at the beginning of the target string
The “$” locator specifies that the matching pattern must appear at the end of the target object
The ‘\ B’ locator specifies that the matching pattern must appear at one of the two boundaries at the beginning or end of the target string
The “\ B” locator specifies that the matching object must be within the two boundaries of the beginning and end of the target string,
That is, the matching object cannot be the beginning or the end of the target string.
Similarly, we can also regard “^” and “$” as two groups of locators which are reciprocal operations. For example: / ^ hell / because the regular expression above contains a “^” locator, it can match a string starting with “hell”, “hello” or “hellhound” in the target object/ Ar $/ because the regular expression above contains a “$” locator, it can match the string ending with “car”, “bar” or “ar” in the target object/\ Bbom / because the above regular expression pattern starts with the “ B” locator, it can match the string that starts with “bomb” or “BOM” in the target object/ Because the above regular expression pattern ends with the “ B” locator, it can match the string ending with “human”, “woman” or “man” in the target object.
In order to facilitate the user to set the matching pattern more flexibly, regular expression allows the user to specify a certain range in the matching pattern, not limited to specific characters. For example:
/[A-Z] / the above regular expression will match any upper case letter from a to Z.
/[A-Z] / the above regular expression will match any lowercase letter from a to Z.
/[0-9] / the above regular expression will match any number from 0 to 9.
/([A-Z] [A-Z] [0-9]) + / the above regular expression will match any string composed of letters and numbers, such as “Ab0”.

Here we need to remind users that “()” can be used in regular expressions to combine strings together. “()” The contents of the symbol must appear in the target object at the same time. Therefore, the above regular expression will not match a string such as “ABC” because the last character in “ABC” is a letter rather than a number.
If we want to implement the “or” operation similar to programming logic in regular expressions, and choose any one of several different patterns for matching, we can use the pipe character “|”. For example, / to | too | 2 / the regular expression above will match “to”, “too”, or “2” in the target object.
There is also a common operator in regular expressions, which is the negative character “[^]”. Different from the locator “^”, 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 speaking, when “^” appears in “[]”, it is regarded as a negative operator; When “^” is outside “[]”, or there is no “[]”, it should be regarded as a locator.
Finally, the escape character “\” can be used when users need to add metacharacters to the pattern of regular expressions and find their matching objects. For example, / th \ * / the above regular expression will match “th *” instead of “the” in the target object.

After the regular expression is constructed, it can be evaluated like a mathematical expression, that is, it can be evaluated from left to right in a priority order. The priorities are as follows:

1. Escape character
2.(), (?:), (?=), [] parentheses & square brackets
3.*, +, ?, { n} , {n,}, {n, m} qualifiers
4. ^, $, [anymetacharacter position and order
5. | “or” operation

Use examples

JavaScript 1.2 has a powerful regexp () object, which can be used to match regular expressions. The test () method can check whether the target object contains matching patterns, and return 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 object
This object contains regular expression patterns and flags that show how to apply patterns.
Syntax 1 re = / pattern / [flags]
Syntax 2 re = new regexp (“pattern”, [“flags”])
Parameters
  re
Required. The name of the variable to be assigned to the regular expression pattern.
  Pattern
Required. The regular expression pattern to use. If you use syntax 1, separate the patterns with the ‘/’ character. If you use syntax 2, use quotation marks to cause the pattern.
  Flags
Optional. If you use syntax 2, use quotation marks to enclose the flag. Signs can be used in combination
G (find all patterns in full text)
I (ignore case)
M (multi row lookup)

Examples

The following example creates an object (RE) containing a regular expression pattern and related flags to show you how to use a regular expression object. In this case, the resulting regular expression object is used in the match method

Copy codeThe code is as follows:
function MatchDemo()
  {
  var r, re; // Declare variables.
  var s = “The rain in Spain falls mainly in the plain”;
  re = new RegExp(“ain”,”g”); // Create a regular expression object.
  r = s.match(re); // Find a match in the string s.
  return(r);
  }

The following are the specific uses:

Method 1
Var re = / regular expression /;
re.test($(“txtid”).val())

Method 2
$(“txtid”). Val.match (/ regular expression /);

Regular expressions come in many different styles. The following table is a complete list of metacharacters in PCRE and their behavior in the context of regular expressions:

character describe
\ Marks the next character as a special character, an original character, a backward reference, or an octal escape character. For example, “n” matches the character “n”. “\ “N” matches a newline character. The sequence “\ \” matches “\” and “\ (” matches “(“.
^ Matches the start of the input string. If the multiline property of the regexp object is set, ^ also matches the position after “\ n” or “\ R”.
$ Matches the end position of the input string. If the multiline property of the regexp object is set, the $also matches the position before “\ n” or “\ R”.
* Matches the preceding subexpression zero or more times. For example, Zo * can match “Z” and “zoo.”* It is equivalent to {0,}.
+ Matches the previous subexpression one or more times. For example, “Zo +” can match “Zo” and “zoo”, but not “Z”+ It is equivalent to {1,}.
? Matches the previous subexpression zero or once. For example, “do (ES)” Can match “do” in “do” or “does”.? It is equivalent to {0,1}.
{n} N is a nonnegative integer. Match the determined n times. For example, “O {2}” cannot match “O” in “Bob”, but it can match two o’s in “food”.
{n,} N is a nonnegative integer. Match at least N times. For example, “O {2,}” cannot match “O” in “Bob”, but it 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. The minimum number of matches is n and the maximum number of matches is m. For example, “O {1,3}” will match the first three o’s in “food.”“ O {0,1} is equivalent to “O?”. Note that there cannot be a space between a comma and two numbers.
? When the character is immediately followed by any other qualifier (*, +{ n} When {n,}, {n, m}), the matching pattern is non greedy. The non greedy pattern matches as few strings as possible, while the default greedy pattern matches as many strings as possible. For example, for the string “oooo”, “O + Will match a single “O”, and “O +” will match all “O”.
. Matches any single character except ‘\ n’. To match any character, including “\ n”, use a pattern like “[. [n]”.
(pattern) Match the pattern and get the match. The obtained matches can be obtained from the generated matches set. The submatches set is used in VBScript, and the $0… $9 attribute is used in JScript. To match the parenthesis character, use “\ (” or “\)”.
(?:pattern) The pattern is matched but the matching result is not obtained, that is to say, it is a non obtained matching and is not stored for later use. This is useful when using the or character “(|)” to combine parts of a pattern. For example, “industr (?): Y | ies) “is a simpler expression than” industry | industries “.
(?=pattern) Forward lookup, which matches the search string at the beginning of any string that matches the pattern. This is a non fetch match, that is, the match does not need to be fetched for later use. For example, “windows (?)= 95 | 98 | NT | 2000) “can match” windows “in” Windows 2000 “, but cannot match” windows “in” windows 3.1 “. Pre query does not consume characters, that is to say, after a match occurs, the next matching search starts immediately after the last matching, rather than after the characters containing the pre query.
(?!pattern) Negative look-up, which matches the search string at the beginning of any string that does not match pattern. This is a non fetch match, that is, the match does not need to be fetched for later use. For example, “windows (?)?! 95 | 98 | NT | 2000) “can match” windows “in” windows3.1 “, but cannot match” windows “in” Windows2000 “. Pre query does not consume characters, that is to say, after a match occurs, the next matching search starts immediately after the last matching, rather than after the characters containing the pre query
x|y Match X or y. For example, “Z | food” can match “Z” or “food”“( “Z | f) ood” matches “zood” or “food”.
[xyz] Character set. Match any character contained. For example, “ABC” can match “a” in “plain”.
[^xyz] Set of negative characters. Matches any character that is not included. For example, ‘[^ ABC]’ can match ‘p’ in ‘plain’.
[a-z] Character range. Matches any character in the specified range. For example, “[A-Z]” can match any lowercase character in the range of “a” to “Z”.
[^a-z] Negative character range. Matches any character that is not in the specified range. For example, ‘[^ A-Z]’ can match any character that is not in the range of ‘a’ to ‘Z’.
\b Match a word boundary, that is, the position between the word and the space. For example, “Er / b” can match “Er” in “never”, but not “Er” in “verb”.
\B Match non word boundaries“ “Er / b” can match “Er” in “verb”, but cannot match “Er” in “never”.
\cx Matches the control character indicated by X. For example, \% cm matches a control-m or carriage return. The value of X must be one of A-Z or A-Z. Otherwise, C is regarded as a literal “C” character.
\d Matches a numeric character. Equivalent to [0-9].
\D Matches a non numeric character. Equivalent to [^ 0-9].
\f Match a page feed. It’s equivalent to ⁃ x0c and ⁃ CL.
\n Matches a newline character. It’s equivalent to ⁃ x0a and ⁃ CJ.
\r Matches a carriage return. It’s equivalent to ⁃ x0d and ⁃ cm.
\s Matches any white space characters, including spaces, tabs, page breaks, and so on. It is equivalent to [[f / N / R / T / v].
\S Matches any non white space characters. It is equivalent to [^ ﹣ f ﹣ n ﹣ R ﹣ t ﹣ v].
\t Matches a tab. It’s equivalent to ⁃ X09 and ⁃ CI.
\v Matches a vertical tab. It’s equivalent to ⁃ x0B and ⁃ CK.
\w Matches any word character that includes an underline. It is equivalent to “[a-za-z0-9]”.
\W Matches any non word characters. Equivalent to “[^ a-za-z0-9]”.
\xn Matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be two digits long. For example, “x41” matches “a”. “\ “X041” is equivalent to “X04 & 1”. ASCII encoding can be used in regular expressions
\num Match num, where num is a positive integer. A reference to the obtained match. For example, ‘(.) \ 1’ matches two consecutive identical characters.
\n Identifies an octal escape value or a backward reference. If there are at least n acquired subexpressions before, then n is a backward reference. Otherwise, if n is an octal digit (0-7), then n is an octal escape value.
\nm Identifies an octal escape value or a backward reference. Nm is a backward reference if there are at least nm derived subexpressions before  nm. If there are at least n fetches before, then n is a backward reference followed by the character M. If the previous conditions are not satisfied, if n and m are octal digits (0-7), then the escape value nm will match the octal escape value nm.
\nml If n is an octal number (0-3), and m and L are both octal numbers (0-7), the octal escape value NML is matched.
\un Matches n, where n is a Unicode character represented by four hexadecimal digits. For example, u00a9 matches the copyright symbol (?).

Attached:

Regular expression set for verifying numbers (Reprint)
Verification number: ^ [0-9]*$
Verify n-digit numbers: ^ {D {n}$
Verify at least n digits: ^ – D {n,}$
Verify the number of M-N bits: ^ – D {m, n}$
Verify zero and non-zero numbers: ^ (0 | [1-9] [0-9] *)$
Verify the positive real number with two decimal places: ^ [0-9] + (. [0-9] {2})$
Verify the positive real number with 1-3 decimal places: ^ [0-9] + (. [0-9] {1,3})$
Verify non-zero positive integers: ^ \ +[ 1-9][0-9]*$
Verify non-zero negative integers: ^ \ – [1-9] [0-9]*$
Verify non negative integers (positive integers + 0) ^ D+$
Verify non positive integers (negative integers + 0) ^ ((- – D +) | (0 +))$
Verify characters of length 3: ^. {3}$
Verify the 26 letter string: ^ [a-za-z]+$
Verify a string of 26 uppercase English letters: ^ [A-Z]+$
Verify the string composed of 26 lowercase English letters: ^ [A-Z]+$
Verify the string composed of numbers and 26 English letters: ^ [a-za-z0-9]+$
Verify a string of numbers, 26 letters, or underscores: ^ \+$
Verify the user password: ^ [a-za-z] [w {5,17} $the correct format is: it starts with a letter, the length is between 6-18, and can only contain characters, numbers and underscores.
Verify whether it contains ^% & ‘; =$\“ Equal characters: [^% & ‘; =$\ x22]+
Verification of Chinese characters: ^ [[u4e00 – [u9fa5], {0,}$
Verify email address: ^ – W + [-.] – W +) * @ – W + ([-.] – W +) * \. W + ([-.] – W +)*$
Verify the Internet URL: ^ http: / / ([[w -] + \.) + [[w -] + (/ [[w -. /?%, = = *)? $^[ a-zA-z]+://(w+(-w+)*)(.(w+(-w+)*))*(? S*)?$
Verify the phone number: ^ (\ ([D {3,4}) | [D {3,4} -)\ D {7,8} $: — the correct format is: xxx-xxxxxxxx, xxx-xxxxxxxx, xxx-xxxxxxxx, xxx-xxxxxxxx, XXXXXXXX, XXXXXXXX.
Verify ID number (15 digit or 18 digit): ^\d{15}|\d{}18$
12 months of validation year: ^ (0[ 1-9] | 1 [0-2]) $the correct format is: “01” – “09” and “1” “12”
Verify the 31 days of a month: ^ ((0[ The correct format is: 01,09 and 1,31.
Integer: ^ -\ d+$
Non negative floating-point number (positive floating-point number + 0): ^ + D + (\. [D +)$
Positive floating point number ^ (([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *)$
Non positive floating-point number (negative floating-point number + 0) ^ ((- – D + (\. [D +)?))|( 0+(\.0+)?))$
Negative floating point number ^ (- (([0-9] + \. [0-9] * [1-9] [0-9] *); ([0-9] * [1-9] [0-9] * \. [0-9] +); ([0-9] * [1-9] [0-9] *))$
Floating point number ^ (-)\ d+)(\.\d+)?

The following is the specific function writing, will not, you can refer to the next.

Copy codeThe code is as follows:
function check()
{
var bb = document.getElementById(“txt_ id”).value;// txt_ ID is the ID of the text box
alert(ismobile(bb));// Ismobile stands for any of the following function names
}
HTML code:
<input type=”text” name=”textfield” />
< input type = “submit” name = “submit” value = “onclick =” check() “/ >
**************************/
//Determine whether the input is a string composed of 0-9 / A-Z / A-Z
function isalphanumber(str)
{
var result=str.match(/^[a-zA-Z0-9]+$/);
if(result==null) return false;
return true;
}

//Judge whether the input is a number — (number contains decimal)–
function isnumber(str)
{
return !isNaN(str);
}

//Determine whether the input is an integer
function isint(str)
{
var result=str.match(/^(-|\+)?\d+$/);
if(result==null) return false;
return true;
}

//Judge whether the input is a valid long date format – “yyyy-mm-dd HH: mm: SS” | “yyyy / mm / DD HH: mm: SS”
function isdatetime(str)
{
var result=str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
if(result==null) return false;
var d= new Date(result[1], result[3]-1, result[4], result[5], result[6], result[7]);
return (d.getFullYear()==result[1]&&(d.getMonth()+1)==result[3]&&d.getDate()==result[4]&&d.getHours()==result[5]&&d.getMinutes()==result[6]&&d.getSeconds()==result[7]);
}

//Check whether the date format is yyyy-mm-dd | | yyyy / mm / DD
function isdate(str){
var result=str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(result==null) return false;
var d=new Date(result[1], result[3]-1, result[4]);
return (d.getFullYear()==result[1] && d.getMonth()+1==result[3] && d.getDate()==result[4]);
}

//Determine whether the input is a valid email
function isemail(str)
{
var result=str.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
if(result==null) return false;
return true;
}

//Remove the spaces at the beginning and end of a string
function trim(str){
return str.replace(/(^\s*)|(\s*$)/g, “”);
}

//Returns the actual length of the string. One Chinese character counts as two lengths
function strlen(str){
return str.replace(/[^\x00-\xff]/g, “**”).length;
}

//Postcode of China (6 digits)
function ispostcode(str)
{
var result=str.match(/[1-9]\d{5}(?!\d)/);
if(result==null) return false;
return true;
}
//Match domestic telephone number (0511-4405222 or 021-87888822)
function istell(str)
{
var result=str.match(/\d{3}-\d{8}|\d{4}-\d{7}/);
if(result==null) return false;
return true;
}

//Check whether it is an integer of (0-10000)
function isint1(str)
{
var result=str.match(/^[0-9]$|^([1-9])([0-9]){0,3}$|^10000$/);
if(result==null) return false;
return true;
}

//Match Tencent QQ number
function isqq(str)
{
var result=str.match(/[1-9][0-9]{4,}/);
if(result==null) return false;
return true;
}

//Matching ID card (15 or 18)
function isidcard(str)
{
var result=str.match(/\d{15}|\d{18}/);
if(result==null) return false;
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Check whether the text is empty
function checknull(field,sval)
{
if (field.value ==””)
{
Alert (“please fill in” + sval + “!”);
field.focus();
return false;
}
return true;
}

//Mask input characters
/***********************
Call method:
In the text box, add onkeypress = “return checkchar()”
*************************/
function checkChar()
{
var keycode = event.keyCode;
if(!(keycode>=48&&keycode<=57))
{
return false;
}
}

/***************************************************************************************************************************
China phone number verification
Matching form: 0511-4405222 or 021-87888822 or 021-44055520-555 or (0511) 4405222
Regular expression “((D {3,4}) | D {3,4} -)? d{7,8}(-d{3})*”

China postcode verification
The matching form is 215421
Regular expression “d {6}”

Email verification
The matching form is as follows :[email protected]
Regular expression “W + ([- +.] W +) * @ w + ([-.] W +) *. W + ([-.] W +) *”

ID card verification
Matching form, such as: 15 or 18 ID card
Regular expression “d {18} | D {15}”

Common digital verification
regular expression
“D {n}” n is the specified length
The length range of “d {n, m}” n to M

Illegal character verification
Match illegal characters such as: < > & / ‘|
Regular expression [^ < > & / | ‘\]+

Date validation
Matching form: 20030718030718
Range: 1900-2099
Regular expression ((((19) {1} | (20) {1}) d {2}) | D {2}) [01] {1} D {1} [0-3] {1} D {1}

Regular expressions matching Chinese characters: [[u4e00 – [u9fa5]
Commentary: matching Chinese is really a headache, with this expression is easy to do

Matching double byte characters (including Chinese characters): [^ / X00 – / XFF]
Comment: can be used to calculate the length of the string (a double byte character length of 2, ASCII characters of 1)

Regular expressions that match blank lines are as follows: \ \ n / s * \ \ R
Comment: can be used to delete blank lines

Regular expressions matching HTML Tags: < (< s *?) [^ >] * >. *? | <. *? / >
Comment: the version circulated on the Internet is too bad. The above one can only match the part, and it is still powerless for complex nested tags

Regular expressions that match the first and last white space characters: ^ – s * | – S*$
Comments: can be used to delete the beginning and end of the line of white space characters (including spaces, tabs, page breaks, etc.), very useful expressions

Regular expression matching email address: W + ([-.] – W +) * @ \ W + ([-.] – W +) * \. W + ([-.] – W +))*
Comment: form validation is very useful

Regular expression matching URL: [a-za-z] +: / / [^ s]*
Commentary: the function of the version circulated on the Internet is very limited, and the above one can basically meet the needs

Whether the matching account number is legal (starting with a letter, 5-16 bytes are allowed, and alphanumeric underscores are allowed): ^ [a-za-z] [a-za-z0-9]{ 4,15}$
Comment: form validation is very useful

Match domestic phone number: D {3} – [D {8} | [D {4} – [D {7}
Comments: the matching form is 0511-4405222 or 021-87888822

Match Tencent QQ number: [1-9] [0-9] {4,}
Commentary: Tencent QQ number starts from 10000

The postcode of China: [1-9] [D {5} (?)\ d)
Comment: the postcode of China is 6 digits

Matching ID card: D {15} | – D {18}
Commentary: China’s ID card is 15 or 18

Matching IP address: D + \. [D + \. [D + \. [D + \. [D]+
Comment: useful for extracting IP address

Extract the IP address in the information:
(\d+)\.(\d+)\.(\d+)\.(\d+)

Extract the Chinese mobile phone number in the information:
(86)*0*13\d{9}

Extract China fixed telephone number in the information:
(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}

Extract the Chinese telephone number (including mobile and fixed telephone) in the information:
(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}

Extract the postcode of China in the information:
[1-9]{1}(\d+){5}

The ID number of China in the information extraction:
\d{18}|\d{15}

Extract the integer in the information:
\d+

Extract floating point numbers (i.e. decimals) in the information:
(-?\d*)\.?\d+

Extract any number in the information:
(-?\d*)(\.\d+)?

Extract the Chinese string in the information:
[\u4e00-\u9fa5]*

Extract the double byte string (Chinese characters) in the information:
[^\x00-\xff]*

Extract the English string in the information:
\w*
Extract the network links in the information:
(h|H)(r|R)(e|E)(f|F) *= *(‘|”)?(\w|\\|\/|\.)+(‘|”| *|>)?

Extract the email address in the message:
\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

Extract image links in information:
(s|S)(r|R)(c|C) *= *(‘|”)?(\w|\\|\/|\.)+(‘|”| *|>)?

Match specific numbers:
^[1-9] [D * $/ / match positive integers
^-[1-9] [D * $/ / match negative integers
^-?[ 1-9] [D * $/ / match integers
^[1-9] [D * | 0 $/ / matches non negative integers (positive integers + 0)
^-[1-9] [D * | 0 $/ / matches non positive integers (negative integers + 0)
^[1-9] – D * \. [D * | 0 \. [D * [1-9] – D * $/ / matches a positive floating-point number
^-([1-9] – D * \. [D * | 0 \. [D * [1-9] – D * * $/ / matches negative floating point numbers
^-?([ 1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\. 0 + | 0) $/ / match floating point numbers
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\. 0 + | 0 $/ / matches non negative floating point number (positive floating point number + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\. 0 + | 0 $/ / matches non positive floating point number (negative floating point number + 0)
Commentary: it is useful to deal with a large amount of data, and attention should be paid to the correction in specific application

Match specific string:
^[a-za-z] + $/ / matches a string of 26 English letters
^[A-Z] + $/ / matches a string of 26 uppercase English letters
^[A-Z] + $/ / matches a string composed of 26 lowercase English letters
^[a-za-z0-9] + $/ / matches a string of numbers and 26 English letters
^\W + $/ / matches a string of numbers, 26 letters or underscores
Comment: some of the most basic and commonly used expressions

////////////////////The first four lines are used to protect JS code from being downloaded
/// / basic regular expressions///////////////////
//Non null validation function notnull (STR) {return (STR! =);}
//Email address verification
function checkEmail (str) {
//The regular expression of email address isemail1 = / ^ W + ([\. \ -] – W +) * \ @ \ W + ([\. \ -] – W +) * \. [w + $/;
//Email address regular expression isemail2 = / ^. * @ [^] * $/;
//Verify the email address and return the result (isemail1. Test (STR) & isemail2. Test (STR));
}/ / ID card verification function checkidcard (STR){
//ID card regular expression (15 bits)
isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
//ID card regular expression (18 bits) isidcard2 = / ^ [1-9] \ \ D {5} [1-9] \ \ D {3} ((0-D) | (1 [0-2])) (([0 | 1 | 2] \ \ d) | 3 [0-1]) \ \ D {4} $/;
//Verify the ID card and return the result (isidcard1. Test (STR) | isidcard2. Test (STR));}
//IP verification function checkip (STR)
{/ / IP regular expression IP = ‘(25 [0-5] | 2 [0-4] \ \ D | 1 \ \ D \ \ D | \ \ D \ \ D | \ \ d)’;
IPdot=IP+’\\.’; isIPaddress=new RegExp(‘^’+IPdot+IPdot+IPdot+IP+’$’);
//Verify the IP and return the result return (isipaddress. Test (STR));}
//Function checkhomepage (STR){
//Home page regular expression / / ishomepage = / ^ W + ([\. \ -] – W) * $/; isHomepage=/^\w+(\.\w+)+\.\w+$/;
//Verify the home page and return the result return (ishomepage. Test (STR));}
//Is the number function isnum (STR) {/ / isnumber = / ^ ([1-9] d * (\. [D +)?) (\ d+(\.\d+))$/; isNumber=/^\d+(\.\d+)?$/;
//Verify and return the result return (isnumber. Test (STR));}
//Is integer function isint (STR) {isinteger = / ^ / D + $/;
//Verify and return the result return (isinteger. Test (STR));}
//Whether the letter function ischar (STR) {ischaracter = / ^ [a-za-z] + $/;
//Verify and return the result return (ischaracter. Test (STR));}
/////////////////////Basic pop ups///////////////////
function checkBoolean(bv,i,w) { if(bv==false) { try{i.focus();}catch(e){} alert(w); return false; } return true }
////////////////////Element and value judgment//
Function checkelement selected_ selected(item,alert_ str) { if(item.type==”select-one”)return checkElement_ NotNull(item,alert_ str); if(alert_ str.length==0)alert_ STR = item. Title + “required!”; rt=false; if(item.length>0) { for(i=0; i<item.length; i++){rt=rt||item[i].checked;} } else { rt=item.checked } return checkBoolean(rt,item[0],alert_ str); return true; } //
Not empty function checkelement_ NotNull(a,alert_ str,g) { v=a.value; w=alert_ str; if(alert_ Str.length = = 0) w = a.title + “cannot be empty!”; return(checkValue_ NotNull(v,a,w,g)); } function checkValue_ NotNull(v,i,w,g) { if(g!=” NOT_ TRIM”)v=v.replace(/(^\s*)|(\s*$)/g, “”); bv=NotNull(v); return(checkBoolean(bv,i,w)); }
//Legal mailbox function checkelement_ IsEmail(a,alert_ str,g) { v=a.value; w=alert_ str; if(alert_ Str.length = = 0) w = a.title + “cannot be empty!”; return(checkValue_ IsEmail(v,a,w,g)); }
function checkValue_ IsEmail(v,i,w,g) { if(g!=” NOT_ TRIM”)v=v.replace(/(^\s*)|(\s*$)/g, “”); bv=checkEmail(v); return(checkBoolean(bv,i,w)); } // Legal ID card function checkelement_ IsIDCard(a,alert_ str,g) { v=a.value; w=alert_ str; if(alert_ Str.length = = 0) w = a.title + “cannot be empty!”; return(checkValue_ IsIDCard(v,a,w,g)); }
function checkValue_ IsIDCard(v,i,w,g) { if(g!=” NOT_ TRIM”)v=v.replace(/(^\s*)|(\s*$)/g, “”); bv=checkIDCard(v); return(checkBoolean(bv,i,w)); } // Legal IP function checkelement_ IsIP(a,alert_ str,g) { v=a.value; w=alert_ str; if(alert_ Str.length = = 0) w = a.title + “cannot be empty!”; return(checkValue_ IsIP(v,a,w,g)); } function checkValue_ IsIP(v,i,w,g) { if(g!=” NOT_ TRIM”)v=v.replace(/(^\s*)|(\s*$)/g, “”); bv=checkIP(v); return(checkBoolean(bv,i,w)); }
//Verify digital function checkelement_ IsNum(a,alert_ str,g) { v=a.value; w=alert_ str; if(alert_ Str.length = = 0) w = a.title + “cannot be empty!”; return(checkValue_ IsNum(v,a,w,g)); } function checkValue_ IsNum(v,i,w,g) { if(g!=” NOT_ TRIM”)v=v.replace(/(^\s*)|(\s*$)/g, “”); bv=isNum(v); return(checkBoolean(bv,i,w)); }
//Verify integer function checkelement_ IsInt(a,alert_ str,g) { v=a.value; w=alert_ str; if(alert_ Str.length = = 0) w = a.title + “cannot be empty!”; return(checkValue_ IsInt(v,a,w,g)); } function checkValue_ IsInt(v,i,w,g) { if(g!=” NOT_ TRIM”)v=v.replace(/(^\s*)|(\s*$)/g, “”); bv=isInt(v); return(checkBoolean(bv,i,w)); } // Verify letter function checkelement_ IsChar(a,alert_ str,g) { v=a.value; w=alert_ str; if(alert_ Str.length = = 0) w = a.title + “cannot be empty!”; return(checkValue_ IsChar(v,a,w,g)); } function checkValue_ IsChar(v,i,w,g) { if(g!=” NOT_ TRIM”)v=v.replace(/(^\s*)|(\s*$)/g, “”); bv=isChar(v); return(checkBoolean(bv,i,w)); }
//Legal homepage function checkelement_ IsHomepage(a,alert_ str,g) { v=a.value; w=alert_ str; if(alert_ Str.length = = 0) w = a.title + “cannot be empty!”; return(checkValue_ IsHomepage(v,a,w,g)); } function checkValue_ IsHomepage(v,i,w,g) { if(g!=” NOT_ TRIM”)v=v.replace(/(^\s*)|(\s*$)/g, “”); bv=checkHomepage(v); return(checkBoolean(bv,i,w)); }

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]