Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic syntax of regular expressions

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what are the basic grammars of regular expressions", which is easy to understand and well organized. I hope it can help you solve your doubts. Let me lead you to study and learn the article "what are the basic grammars of regular expressions".

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" respectively.

"more", "once or more" and "none or once". 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'', which means "or" operation:

"hihello": indicates "hi" or "hello" in a string

"(bcd) ef": indicates "bef" or "cdef"

"(ab) * 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 "ab")

"[amurd]": indicates that a string contains one of the lowercase'a'to'd'(equivalent to "abcd" or "[abcd]")

"^ [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]%" table

There should be no letters 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] {1pm 20} $/

If (! patrn.exec (s)) return false

Return 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] | [. _]) {4pm 19} $/

If (! patrn.exec (s)) return false

Return true

}

/ / verify user name: only 1-30 strings that begin with letters can be entered

Function isTrueName (s)

{

Var patrn=/ ^ [a-zA-Z] {1pm 30} $/

If (! patrn.exec (s)) return false

Return true

}

/ / check password: only 6-20 letters, numbers and underscores can be entered

Function isPasswd (s)

{

Var patrn=/ ^ (\ w) {6pm 20} $/

If (! patrn.exec (s)) return false

Return true

}

/ / check ordinary telephone and fax numbers: it can start with "+" and contain "-" in addition to numbers.

Function isTel (s)

{

/ / var patrn=/ ^ [+] {0rect 1} (\ d) {1je 3} []? ([-]? (\ d) {1je 12}) + $/

Var patrn=/ ^ [+] {0jue 1} (\ d) {1JI 3} []? ([-]? ((\ d) | []) {1Jet 12}) + $/

If (! patrn.exec (s)) return false

Return 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=/ ^ [+] {0jue 1} (\ d) {1JI 3} []? ([-]? ((\ d) | []) {1Jet 12}) + $/

If (! patrn.exec (s)) return false

Return true

}

/ / check Postal Code

Function isPostalCode (s)

{

/ / var patrn=/ ^ [a-zA-Z0-9] {3Jing 12} $/

Var patrn=/ ^ [a-zA-Z0-9] {3pm 12} $/

If (! patrn.exec (s)) return false

Return true

}

/ / verify search keywords

Function isSearch (s)

{

Var patrn=/ ^ [^ `~! @ # $% ^ & * () + = |\\] [\]\]\ {\}:;\',. /?] {1} [^` ~! @ $% ^ & () + = |\\] [\]\]\ {\}:;\',.

If (! patrn.exec (s)) return false

Return true

}

Function isIP (s) / / by zergling

{

Var patrn=/ ^ [0-9.] {1J 20} $/

If (! patrn.exec (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 = theStr.indexOf ('-')

Var the2nd = theStr.lastIndexOf ('-')

If (the1st = = the2nd) {return (false);}

Else {

Var y = theStr.substring (0pm the 1st)

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

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 (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 (0pm the 1st)

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

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 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 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=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

These are all the contents of the article "what are the basic grammars of regular expressions?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report