In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the js regular expression basic grammar example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
1. Basic syntax of regular expressions
Two special symbols'^ 'and' $'. Their function is to indicate the beginning and end of a string, respectively.
Examples are as follows:
"^ The": represents all strings starting with "The" ("There", "The cat", etc.)
"of despair$": a string that therefore ends in "of despair"
"^ abc$": a string that starts and ends with "abc"-- hehe, only "abc" itself.
"notice": represents any string that contains "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.
Others are'*','+ 'and'?' These three symbols represent the number of repetitions of a character or a sequence of characters.
They mean "no or more", "once or more" and "no or once", respectively.
Here are a few examples:
"ab*": indicates that a string has an a followed by zero or more b. "a", "ab", "abbb",. )
"ab+": indicates that a string has an a followed by at least one b or more
"ab?": indicates that a string has an a followed by zero or a b
"a blank $": indicates that there are zero or one a followed by one or more b at the end of the string.
You can also use ranges, enclosed in curly braces, to indicate the range of repetitions.
"ab {2}": indicates that a string has an a followed by two b ("abb")
"ab {2,}": indicates that a string has an a followed by at least 2 b
"ab {3pr 5}": indicates that a string has an a followed by 3 to 5 b.
Please note that you must specify the lower limit of the range (for example: "{0jue 2}" instead of "{, 2}").
Also, you may have noticed,'*','+ 'and'?' Equivalent to "{0,}", "{1,}" and "{0jin1}".
There is also a "OR" operation, which means "OR":
"hi hello": indicates that there is "hi" or "hello" in a string
"(baked cd) ef": indicates "bef" or "cdef"
"(a roomb) * c": indicates a string of "a" and "b" mixed strings followed by a "c"
'.' You can replace any character:
"a. [0-9]": indicates that a string has an "a" followed by an arbitrary character and a number
"^. {3} $": represents a string of any three characters (3 characters in length)
Square brackets indicate that certain characters are allowed to appear at a specific location in a string:
"[ab]": indicates that a string has a "a" or "b" (equivalent to "a roomb")
"[a murd]": indicates that a string contains one of the lowercase'a'to'd'(equivalent to "an abcd" or "[murd]").
"^ [a-zA-Z]": represents a string that begins with a letter
"[0-9]%": a number that is preceded by a percent sign
", [a-zA-Z0-9] $": indicates that a string ends with a comma followed by a letter or number.
You can also use'^'in square brackets to indicate unwanted characters.'^ 'should be in the first place in square brackets.
(for example: "% [^ a-zA-Z]%" means that letters should not appear in the two percent signs).
In order to express verbatim, you must add the transfer character'\ 'before the characters "^. $() * +? {\".
Note that no escape characters are required in square brackets.
two。 Regular expression validation controls the input character type of the text box
1. You can only enter numbers and English:
two。 You can only enter numbers:
3. You can only enter full-width:
4. Those who can only enter Chinese characters:
3. A popular illustration of the application of regular expressions
*
/ / check whether it is made up of numbers.
/ ^ [0-9] {1pm 20} $/
^ means that the character that starts with it matches the rule immediately following ^.
$means that the character that starts with it matches the rule immediately before $.
The content in [] is an optional character set
[0-9] indicates that the required character range is between 0-9.
{1 # 20} means that the length of the numeric string is legally 1 to 20, that is, the number of occurrence of the characters in [0-9] ranges from 1 to 20.
The paired use of / ^ and $/ should indicate a rule that requires the entire string to exactly match the definition, rather than just one substring in the string.
*
/ / check login: you can only enter 5-20 characters that start with a letter, can be numbered, "_", "." String of
/ ^ [a-zA-Z] {1} ([a-zA-Z0-9] | [. _]) {4pm 19} $/
^ [a-zA-Z] {1} indicates that the first character is required to be a letter.
([a-zA-Z0-9] | [. _]) {4jue 19} represents a string of 4 to 9 bits in length starting from the second bit (because it follows the previous expression) and requires uppercase and lowercase letters, numbers, or a special character set [. _].
*
/ / verify user name: only 1-30 strings that begin with letters can be entered
/ ^ [a-zA-Z] {1pc30} $/
*
/ / check password: only 6-20 letters, numbers and underscores can be entered
/ ^ (\ w) {6pm 20} $/
\ w: used to match alphabetic, numeric or underscore characters
*
/ / check ordinary telephone and fax numbers: can be "+" or start with a number, and can contain "-" and "".
/ ^ [+] {0jue 1} (\ d) {1jue 3} []? ([-]? ((\ d) | []) {1Jet 12}) + $/
\ d: used to match numbers from 0 to 9
"?" The metacharacter states that its leading object must appear zero or once in a row in the target object.
Strings that can be matched, such as: + 123-999999; + 123-999999; + 123 999999; + 123 999999, etc.
*
/ / verify URL
/ ^ http [s] {0http 1}:\ /\ /. + $/ or / ^ http [s] {0Magne1}:\ /. {1Magne1} $/ (indicates that the length of the url string is length ("https://") + n)"
\ /: represents the character "/".
. Represents a set of all characters
+ equals {1,}, which means 1 to positive infinity.
*
/ / verify pure Chinese characters
/
^ [\ u4E00 -\ u9FA5] + $/
[\ u4E00 -\ u9FA5]: it is estimated to be the scope of the Chinese character set
The above expressions are tested and passed in the following javascript
Regular expression:
(fill in the expression between / /)
Check string:
4. Application of rule table formula
"^\ integer $" / / 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
"^ -?\ During $" / / 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] *)) $" / / 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] *)) $" / / negative floating point number
"^ (-?\ d +) (\.\ d +)? $" / / floating point number
"^ [A-Za-z] + $" / / A string of 26 letters
"^ [Amurz] + $" / / A string of 26 English letters in uppercase
A string of 26 lowercase letters consisting of "^ [amurz] + $" / /
"^ [A-Za-z0-9] + $" / / A string of numbers and 26 letters
A string of numbers, 26 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
"^ ([wmere.] +) @ ([[0-9] {1Mague 3}. [0-9] {1pr 3}. [0-9] {1pr 3}.) | (([w -] +.)) ([a-zA-Z] {2jue 4} | [0-9] {1Mague 3}) (]?) $" / / Emil
"(dwells -)? (d {4} -? d {7} | d {3} -? d {8} | ^ d {7je 8}) (- d +)? / / phone number
"^ (d {1 1dd 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1 IP 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1 recorder 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1pm 2} | 1dd | 2 [0-4] d | 25 [0-5])
Regular expression of ^ ([0-9A-F] {2}) (- [0-9A-F] {2}) {5} $/ / MAC address
^ [- +]?\ d + (\.\ d +)? $/ / value type regular expression
5.javascript regular expression test
/ / check whether it is made up of numbers.
Function isDigit (s) {var patrn=/ ^ [0-9] {1 patrn.exec 20} $/; if (! patrn.exec (s)) return falsereturn true}
/ / check login: you can only enter 5-20 characters that start with a letter, can be numbered, "_", "." String of
Function isRegisterUserName (s) {var patrn=/ ^ [a-zA-Z] {1} ([a-zA-Z0-9] | [. _]) {4 a-zA-Z0 19} $/; if (! patrn.exec (s)) return falsereturn true}
/ / verify user name: only 1-30 strings that begin with letters can be entered
Function isTrueName (s) {var patrn=/ ^ [a-zA-Z] {1min30} $/; if (! patrn.exec (s)) return falsereturn true}
/ / check password: only 6-20 letters, numbers and underscores can be entered
Function isPasswd (s) {var patrn=/ ^ (\ w) {6 patrn.exec 20} $/; if (! patrn.exec (s)) return falsereturn true}
/ / check ordinary telephone and fax numbers: it can start with "+" and contain "-" in addition to numbers.
Function isTel (s) {/ / var patrn=/ ^ [+] {0jue 1} (\ d) {1Magne 3} []? ([-]? (\ d) {1Magne 12}) + $/; var patrn=/ ^ [+] {0Magne 1} (\ d) {1Mague 3} []? ([-]? (\ d) | []) {1c12}) + $; if (! patrn.exec (s)) return falsereturn true}
/ / verify the mobile phone number: it must start with a number and may contain "-" in addition to the number.
Function isMobil (s) {var patrn=/ ^ [+] {0 patrn.exec 1} (\ d) {1 patrn.exec 3} []? ([-]? (\ d) | []) {1 patrn.exec 12}) + $/; if (! patrn.exec (s)) return falsereturn true}
/ / check Postal Code
Function isPostalCode (s) {/ / var patrn=/ ^ [a-zA-Z0-9] {3 a-zA-Z0 12} $/; var patrn=/ ^ [a-zA-Z0-9] {3 a-zA-Z0 12} $/; if (! patrn.exec (s)) return falsereturn true}
/ / verify search keywords
Function isSearch (s) {var patrn=/ ^ [^ `~! @ # $% ^ & * () + = |\\] [\]\]\ {\}:;\'\,. /?] {1} [^` ~! @ $% ^ & () + = |\] [\] [\]\ {{\}:;\',.,.] {0cm19} $/; if (! patrn.exec (s)) return falsereturn true} function isIP (s) / / by zergling {var patrn=/ [^ 0-9.] {1pm 20} / If (! patrn.exec (s)) return falsereturn 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 = theStr.indexOf ('-') Var the2nd = theStr.lastIndexOf ('-'); if (the1st = = the2nd) {return (false);} else {var y = theStr.substring; var m = theStr.substring (the1st+1,the2nd); var d = theStr.substring (the2nd+1,theStr.length); var maxDays = 31 if (isInt (m) = = false | | isInt (d) = false | | isInt (y) = false) {return (false);} else if (y.length)
< 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 return if (y% 100 = = 0 & & y% 400 > 0) maxDays = 28 return 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 (theStr.length, 8,10) = = false) {return (false) } else {var the1st = theStr.indexOf ('/'); var the2nd = theStr.lastIndexOf ('/'); if (the1st = = the2nd) {return (false);} else {var m = theStr.substring (the1st+1,the2nd); var d = theStr.substring; var y = theStr.substring (the2nd+1,theStr.length); var maxDays = 31 if (isInt (m) = false | isInt (d) = false | | isInt (y) = = false) {return (false);} else if (y.length)
< 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 return if (y% 100 = = 0 & & y% 400 > 0) maxDays = 28 return maxDays = 29;} if (isBetween (d, 1, maxDays) = = false) {return (false);} else {return (true) } / * * FUNCTION: Compare Date! Which is the latestbands * PARAMETERS: lessDate,moreDate AS String* CALLS: isDate,isBetween* RETURNS: TRUE if lessDateDate2) {return (false);} return (true) } / * FUNCTION isEmpty checks if the parameter is empty or null* PARAMETER str AS String* * * / function isEmpty (str) {if ((str==null) | | (str.length==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 iTuno; I 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 e.g Webmaster@hotmail.com* @ sign can appear only once in the email address.****/function isEmail (theStr) {var atIndex = theStr.indexOf ('@') Var dotIndex = theStr.indexOf ('.', atIndex); var flag = true;theSub = theStr.substring (0, dotIndex+1) if ((atIndex)
< 1)||(atIndex != theStr.lastIndexOf('@'))||(dotIndex < atIndex + 2)||(theStr.length Document to open in the new windowhite ->Height of the new windowwide-> Width of the new windowbars-> 1-Scroll bars = YES 0-Scroll Bars = NOresize-> 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=window.open (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);}
"^\\ integer $" / / 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
"^ -?\\ During $" / / 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] *)) $" / / 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] *)) $" / / negative floating point number
"^ (-?\\ d +) (\.\\ d +)? $" / / floating point number
"^ [A-Za-z] + $" / / A string of 26 letters
"^ [Amurz] + $" / / A string of 26 English letters in uppercase
A string of 26 lowercase letters consisting of "^ [amurz] + $" / /
"^ [A-Za-z0-9] + $" / / A string of numbers and 26 letters
A string of numbers, 26 letters, or underscores.
"^ [\\ w -] + (\\ .[\\ w -] +) * @ [\\ w -] + (\\ .[\\ w -] +) + $" / / email address
"^ [a-zA-z] +: / / (\\ w+ (-\\ w+) *) (\. (\\ w+ (-\\ w+) *)) * (\\?\ S*)? $" / / url
Thank you for reading this article carefully. I hope the article "sample Analysis of the basic Grammar of js regular expressions" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.