(Recommended) JS regular knowledge points topic:https:///article/
1.Basic syntax of regular expressions
Two special symbols '^' and '$'. Their function is to point out the beginning and end of a string respectively.
Examples are as follows:
"^The": means all strings starting with "The" ("There", "The cat", etc.);
"of despair$": means a string ending with "of despair";
"^abc$": A string that indicates that the beginning and end are both "abc" - hehe, only "abc" itself;
"Notice": means any string containing "Notice".
Like the last example, if you don't use two special characters, you are indicating that the string you are looking for is in any part of the string being found - you and
Don't position it at a certain top.
There are other three symbols: '*', '+' and '?', which represent the number of times a sequence of characters appears.
They mean "no or more", "one or more", and "no or once".
Here are a few examples:
"ab*": means that a string has an a followed by zero or several b. ("a", "ab", "abbb",...);
"ab+": means that a string has a followed by at least one b or more;
"ab?": means that a string has a followed by zero or a b;
"a?b+$": means that there are zero or a followed by one or several b at the end of the string.
You can also use ranges, enclosed in braces to indicate the range of repetitions.
"ab{2}": means that a string has an a and 2 b ("abb");
"ab{2,}": means that a string has an a followed by at least 2 b;
"ab{3,5}": means that a string has an a followed by 3 to 5 b.
Note that you must specify the lower limit of the range (such as: "{0,2}" instead of "{,2}").
Also, you may have noticed that '*', '+' and '?' are equivalent to "{0,}", "{1,}" and "{0,1}".
There is also a '¦', which means the "or" operation:
"hi¦hello": means that there is "hi" or "hello" in a string;
"(b¦cd)ef": means "bef" or "cdef";
"(a¦b)*c": means a string of mixed strings of "a" and "b" followed by a "c";
'.' can replace any character:
"a.[0-9]": means that a string has a "a" followed by an arbitrary character and a number;
"^.{3}$": represents a string with any three characters (length is 3 characters);
Square brackets indicate that certain characters are allowed to appear at a certain position in a string:
"[ab]": means that a string has a "a" or "b" (equivalent to "a¦b");
"[a-d]": means that a string contains one of 'a' to 'd' in lowercase (equivalent to "a¦b¦c¦d" or "[abcd]");
"^[a-zA-Z]": represents a string starting with a letter;
"[0-9]%": indicates a number with a digit before a percent sign;
",[a-zA-Z0-9]$": means that a string ends with a comma followed by a letter or number.
You can also use '^' in square brackets to indicate characters that you do not want to appear. '^' should be the first position in square brackets.
(For example: "%[^a-zA-Z]%" means that letters should not appear in the two percent signs).
To express verbatim, you must prefix the "^.$()¦*+?{\" characters with the transfer character '\'.
Note that in square brackets, no escape characters are required.
2. Regular expression verification controls the input character type of text box
1. Only enter numbers and English:
<input onkeyup="value=(/[\W]/g,'') " onbeforepaste="('text',('text').replace(/[^\d]/g,''))" ID="Text1" NAME="Text1">
2. Only numbers can be entered:
<input onkeyup="value=(/[^\d]/g,'') " onbeforepaste="('text',('text').replace(/[^\d]/g,''))" ID="Text2" NAME="Text2">
3. Only input full-width:
<input onkeyup="value=(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="('text',('text').replace(/[^\uFF00-\uFFFF]/g,''))" ID="Text3" NAME="Text3">
4. Only Chinese characters can be entered:
<input onkeyup="value=(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="('text',('text').replace(/[^\u4E00-\u9FA5]/g,''))" ID="Text4" NAME="Text4">
3. Common description of application examples of regular expressions
*******************************************************************************
//Check whether it is all composed of numbers
/^[0-9]{1,20}$/
^ means that the character that starts should match the rules immediately after ^
$ means that the character that starts should match the rule immediately before $
The content in [ ] is an optional character set
[0-9] means that the character range is required to be between 0-9
{1,20} means that the length of the numeric string is legally 1 to 20, that is, the number of characters in [0-9] is 1 to 20 times.
/^ and $/ pairwise use should mean that the rule that requires the entire string to match exactly the definition, rather than just matching a substring in the string.
*******************************************************************************
//Check the login name: Only 5-20 strings starting with letters and can be included with numbers, "_", "."
/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/
^[a-zA-Z]{1} means that the first character is required to be a letter.
([a-zA-Z0-9]|[._]){4,19} represents a string of 4 to 9 bits in length starting from the second bit (because it is immediately following the previous expression), which requires that it consists of upper and lower case letters, numbers, or special character sets [._].
*******************************************************************************
//Check user name: Only 1-30 strings starting with letters can be entered
/^[a-zA-Z]{1,30}$/
*******************************************************************************
//Check password: Only 6-20 letters, numbers, and underscores can be entered
/^(\w){6,20}$/
\w: Used to match letters, numbers or underscore characters
*******************************************************************************
//Calculate the normal phone and fax number: it can start with "+" or number, and can contain "-" and "
/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/
\d: used to match numbers from 0 to 9;
"?" metacharacter specifies that its leading object must appear zero or once in a continuous period of time in the target object
Strings that can be matched are: +123 -999 999 ; +123-999 999 ; 123 999 999 ; +123 9999999, etc.
*******************************************************************************
//Check URL
/^http[s]{0,1}:\/\/.+$/ or /^http[s]{0,1}:\/\/.{1,n}$/ (represents that the length of the url string is length("https://") + n)
\ /: means the character "/".
. A set of all characters
+ is equivalent to {1,}, which means 1 is infinity.
*******************************************************************************
//Calculate pure Chinese characters
/
^[\u4E00-\u9FA5]+$/
[\u4E00-\u9FA5]: It is probably the range of Chinese character sets
All the above expressions are tested and passed in the following javascript
<html> <script language="JavaScript"> <!-- function regx(r,s) { if (r == null || r == ""){ return false; } var patrn= new RegExp(r); if ((s)) return true return false } --> </script> <body> <form>
Rule expression:
<input type="input" name="regxStr" value="" > (fill in/ /Expressions between) <br>
Verify string:
<input type="input" name="str" value="" > <input type="button" name="match" value="match" onClick="alert(regx(,));"> </form> </body> </html>
4. Formal expression application
"^\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+)?$"//Non-negative floating point number (positive 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]+)|([0-9]*[1-9][0-9]*))$"//Positive floating point number
"^((-\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]+)|([0-9]*[1-9][0-9]*)))$"//Negative floating point number
"^(-?\d+)(\.\d+)?$"//Floating point number
"^[A-Za-z]+$"//A string composed of 26 English letters
"^[A-Z]+$"//A string composed of 26 English letters capitalization
"^[a-z]+$"//A string composed of 26 English letters lowercase
"^[A-Za-z0-9]+$"//A string composed of numbers and 26 English letters
"^\w+$"//A string composed of numbers, 26 English letters or underscores
"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"//email address
"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$"//url
/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // Year-Month-Day
/^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/ // Month/Day/Year
"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$" //Emil
"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?" //Phone Number
"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$" //IP address
^([0-9A-F]{2})(-[0-9A-F]{2}){5}$ // Regular expression of MAC address
^[-+]?\d+(\.\d+)?$ //Value type regular expression
Regular expression test
//Check whether it is all composed of numbers
function isDigit(s) { var patrn=/^[0-9]{1,20}$/; if (!(s)) return false return true }
//Check the login name: Only 5-20 strings starting with letters and can be included with numbers, "_", "."
function isRegisterUserName(s) { var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/; if (!(s)) return false return true }
//Check user name: Only 1-30 strings starting with letters can be entered
function isTrueName(s) { var patrn=/^[a-zA-Z]{1,30}$/; if (!(s)) return false return true }
//Check password: Only 6-20 letters, numbers, and underscores can be entered
function isPasswd(s) { var patrn=/^(\w){6,20}$/; if (!(s)) return false return true }
//Calculate the normal telephone and fax numbers: it can start with "+", except for the number, it can contain "-"
function isTel(s) { //var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?(\d){1,12})+$/; var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/; if (!(s)) return false return true }
//Calculate the mobile phone number: It must start with a number, except for the number, it can contain "-"
function isMobil(s) { var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/; if (!(s)) return false return true }
//Check the postal code
function isPostalCode(s) { //var patrn=/^[a-zA-Z0-9]{3,12}$/; var patrn=/^[a-zA-Z0-9 ]{3,12}$/; if (!(s)) return false return true }
//Check search keywords
function isSearch(s) { var patrn=/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;\'\,.<>/?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;\'\,.<>?]{0,19}$/; if (!(s)) return false return true } function isIP(s) //by zergling { var patrn=/^[0-9.]{1,20}$/; if (!(s)) return false return true } /********************************************************************************* * FUNCTION: isBetween * PARAMETERS: val AS any value * lo AS Lower limit to check * hi AS Higher limit to check * CALLS: NOTHING * RETURNS: TRUE if val is between lo and hi both inclusive, otherwise false. **********************************************************************************/ function isBetween (val, lo, hi) { if ((val < lo) || (val > hi)) { return(false); } else { return(true); } } /********************************************************************************* * FUNCTION: isDate checks a valid date * PARAMETERS: theStr AS String * CALLS: isBetween, isInt * RETURNS: TRUE if theStr is a valid date otherwise false. **********************************************************************************/ function isDate (theStr) { var the1st = ('-'); var the2nd = ('-'); if (the1st == the2nd) { return(false); } else { var y = (0,the1st); var m = (the1st+1,the2nd); var d = (the2nd+1,); var maxDays = 31; if (isInt(m)==false || isInt(d)==false || isInt(y)==false) { return(false); } else if ( < 4) { return(false); } else if (!isBetween (m, 1, 12)) { return(false); } else if (m==4 || m==6 || m==9 || m==11) maxDays = 30; else if (m==2) { if (y % 4 > 0) maxDays = 28; else if (y % 100 == 0 && y % 400 > 0) maxDays = 28; else maxDays = 29; } if (isBetween(d, 1, maxDays) == false) { return(false); } else { return(true); } } } /********************************************************************************* * FUNCTION: isEuDate checks a valid date in British format * PARAMETERS: theStr AS String * CALLS: isBetween, isInt * RETURNS: TRUE if theStr is a valid date otherwise false. **********************************************************************************/ function isEuDate (theStr) { if (isBetween(, 8, 10) == false) { return(false); } else { var the1st = ('/'); var the2nd = ('/'); if (the1st == the2nd) { return(false); } else { var m = (the1st+1,the2nd); var d = (0,the1st); var y = (the2nd+1,); var maxDays = 31; if (isInt(m)==false || isInt(d)==false || isInt(y)==false) { return(false); } else if ( < 4) { return(false); } else if (isBetween (m, 1, 12) == false) { return(false); } else if (m==4 || m==6 || m==9 || m==11) maxDays = 30; else if (m==2) { if (y % 4 > 0) maxDays = 28; else if (y % 100 == 0 && y % 400 > 0) maxDays = 28; else maxDays = 29; } if (isBetween(d, 1, maxDays) == false) { return(false); } else { return(true); } } } } /******************************************************************************** * FUNCTION: Compare Date! Which is the latest! * PARAMETERS: lessDate,moreDate AS String * CALLS: isDate,isBetween * RETURNS: TRUE if lessDate<moreDate *********************************************************************************/ function isComdate (lessDate , moreDate) { if (!isDate(lessDate)) { return(false);} if (!isDate(moreDate)) { return(false);} var less1st = ('-'); var less2nd = ('-'); var more1st = ('-'); var more2nd = ('-'); var lessy = (0,less1st); var lessm = (less1st+1,less2nd); var lessd = (less2nd+1,); var morey = (0,more1st); var morem = (more1st+1,more2nd); var mored = (more2nd+1,); var Date1 = new Date(lessy,lessm,lessd); var Date2 = new Date(morey,morem,mored); if (Date1>Date2) { return(false);} return(true); } /********************************************************************************* * FUNCTION isEmpty checks if the parameter is empty or null * PARAMETER str AS String **********************************************************************************/ function isEmpty (str) { if ((str==null)||(==0)) return true; else return(false); } /********************************************************************************* * FUNCTION: isInt * PARAMETER: theStr AS String * RETURNS: TRUE if the passed parameter is an integer, otherwise FALSE * CALLS: isDigit **********************************************************************************/ function isInt (theStr) { var flag = true; if (isEmpty(theStr)) { flag=false; } else { for (var i=0; i<; i++) { if (isDigit((i,i+1)) == false) { flag = false; break; } } } return(flag); } /********************************************************************************* * FUNCTION: isReal * PARAMETER: heStr AS String decLen AS Integer (how many digits after period) * RETURNS: TRUE if theStr is a float, otherwise FALSE * CALLS: isInt **********************************************************************************/ function isReal (theStr, decLen) { var dot1st = ('.'); var dot2nd = ('.'); var OK = true; if (isEmpty(theStr)) return false; if (dot1st == -1) { if (!isInt(theStr)) return(false); else return(true); } else if (dot1st != dot2nd) return (false); else if (dot1st==0) return (false); else { var intPart = (0, dot1st); var decPart = (dot2nd+1); if ( > decLen) return(false); else if (!isInt(intPart) || !isInt(decPart)) return (false); else if (isEmpty(decPart)) return (false); else return(true); } } /********************************************************************************* * FUNCTION: isEmail * PARAMETER: String (Email Address) * RETURNS: TRUE if the String is a valid Email address * FALSE if the passed string is not a valid Email Address * EMAIL FORMAT: AnyName@EmailServer ; webmaster@ * @ sign can appear only once in the email address. *********************************************************************************/ function isEmail (theStr) { var atIndex = ('@'); var dotIndex = ('.', atIndex); var flag = true; theSub = (0, dotIndex+1) if ((atIndex < 1)||(atIndex != ('@'))||(dotIndex < atIndex + 2)||( <= )) { return(false); } else { return(true); } } /********************************************************************************* * FUNCTION: newWindow * PARAMETERS: doc -> Document to open in the new window hite -> Height of the new window wide -> Width of the new window bars -> 1-Scroll bars = YES 0-Scroll Bars = NO resize -> 1-Resizable = YES 0-Resizable = NO * CALLS: NONE * RETURNS: New window instance **********************************************************************************/ function newWindow (doc, hite, wide, bars, resize) { var winNew="_blank"; var opt="toolbar=0,location=0,directories=0,status=0,menubar=0,"; opt+=("scrollbars="+bars+","); opt+=("resizable="+resize+","); opt+=("width="+wide+","); opt+=("height="+hite); winHandle=(doc,winNew,opt); return; } /********************************************************************************* * FUNCTION: DecimalFormat * PARAMETERS: paramValue -> Field value * CALLS: NONE * RETURNS: Formated string **********************************************************************************/ function DecimalFormat (paramValue) { var intPart = parseInt(paramValue); var decPart =parseFloat(paramValue) - intPart; str = ""; if ((decPart == 0) || (decPart == null)) str += (intPart + ".00"); else str += (intPart + decPart); return (str); }
"^\\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+)?$"//Non-negative floating point number (positive 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]+)|([0-9]*[1-9][0-9]*))$"//Positive floating point number
"^((-\\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]+)|([0-9]*[1-9][0-9]*)))$"//Negative floating point number
"^(-?\\d+)(\\.\\d+)?$"//Floating point number
"^[A-Za-z]+$"//A string composed of 26 English letters
"^[A-Z]+$"//A string composed of 26 English letters capitalization
"^[a-z]+$"//A string composed of 26 English letters lowercase
"^[A-Za-z0-9]+$"//A string composed of numbers and 26 English letters
"^\\w+$"//A string composed of numbers, 26 English letters or underscores
"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"//email address
"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$"//url
The above is the basic grammar of regular expressions I share with you. I hope it will be helpful for everyone's learning.