In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "what are the classical Javascript regular expressions?". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Regular expressions that match Chinese characters:
The copy code is as follows:
[\ u4e00 -\ u9fa5]
Match double-byte characters (including Chinese characters):
The copy code is as follows:
[^\ X00 -\ xff]
Application: calculates the length of a string (a double-byte character length meter 2 ~ ASCII character counter 1)
The copy code is as follows:
String.prototype.len=function () {return this.replace ([^\ X00 -\ xff] / g, "aa") .length;}
A regular expression that matches a blank line:
The copy code is as follows:
\ n [\ s |] *\ r
A regular expression that matches the HTML tag:
The copy code is as follows:
/. * | /
A regular expression that matches leading and trailing spaces:
The copy code is as follows:
(^\ s*) | (\ sroom$)
Application: there is no trim function like vbscript in javascript, so we can use this expression to implement it, as follows:
The copy code is as follows:
String.prototype.trim = function ()
{
Return this.replace (/ (^\ s *) | (\ s $) / g, "")
}
Decomposing and translating IP addresses using regular expressions
The following is a Javascript program that uses regular expressions to match IP addresses and converts IP addresses to corresponding values:
The copy code is as follows:
Function IP2V (ip)
{
Re=/ (\ d +)\. (\ d +) / g / / A regular expression that matches the IP address
If (re.test (ip))
{
Return RegExp.$1*Math.pow (255jue 3) + RegExp.$2*Math.pow (255pr 2)) + RegExp.$3*255+RegExp.$4*1
}
Else
{
Throw new Error ("Not a valid IP address!")
}
}
However, if the above program does not use regular expressions, but directly use the split function to decompose it may be easier, the program is as follows:
The copy code is as follows:
Var ip= "10.100.20.168"
Ip=ip.split (.)
Alert ("IP value is:" + (ip [0] * 255*255*255+ip [1] * 255*255+ip [2] * 255+ip [3] * 1))
A regular expression that matches the Email address:
The copy code is as follows:
\ w + ([- +.]\ w +) * @\ w + ([-.]\ w +) *.\ w + ([-.]\ w +) *
A regular expression that matches the URL URL:
The copy code is as follows:
Http://([\w-]+\.)+[\w-]+(/[\w-. /?% & =] *)?
An algorithmic program that uses regular expressions to remove repeated characters from a string: [* Note: this program is incorrect]
The copy code is as follows:
Var s = "abacabefgeeii"
Var s1=s.replace (/ (.). *\ 1zag, "$1")
Var re=new RegExp ("[" + S1 + "]", "g")
Var s2=s.replace (re, "")
Alert (s1+s2) / / result: abcefgi
* Note
= =
If var s = "abacabefggeeii"
The result is wrong, the result is: abeicfgg
The ability of regular expressions is limited
= =
I originally posted on CSDN looking for an expression to remove repetitive characters, but I didn't find it. This is the simplest implementation I can think of. The idea is to use the backward reference to take out the characters including repetition, then create a second expression with the repeated characters, take the non-repetitive characters, and concatenate the two. This method may not apply to strings that require character order.
The javascript program that extracts the file name from the URL address with a regular expression, the following result is page1
The copy code is as follows:
S = "http://blog.penner.cn/page1.htm"
S=s.replace (/ (. *\ /) {0,} ([^\.] +). * / ig, "$2")
Alert (s)
Use regular expressions to restrict text box input in a web form:
Use regular expressions to restrict input to Chinese only:
The copy code is as follows:
Onkeyup= "value=value.replace (/ [^\ u4E00 -\ u9FA5] / g text'')" onbeforepaste=clipboardData.setData ('text',clipboardData.getData (' text'). Replace (/ [^\ u4E00 -\ u9FA5] / g charge')) "
Use regular expressions to restrict the input of full-width characters:
The copy code is as follows:
Onkeyup= "value=value.replace (/ [^\ uFF00-\ uFFFF] / gjime')" onbeforepaste=clipboardData.setData ('text',clipboardData.getData (' text') .replace (/ [^\ uFF00-\ uFFFF] / gjime')) "
Use regular expressions to limit input to numbers:
The copy code is as follows:
Onkeyup= "value=value.replace (/ [^\ d] / gjinger')" onbeforepaste= "clipboardData.setData ('text',clipboardData.getData (' text'). Replace (/ / [^\ d] / gjime'))"
Use regular expressions to limit input to numbers and English:
The copy code is as follows:
Onkeyup= "value=value.replace (/ [\ W] / GMagneur')" onbeforepaste= "clipboardData.setData ('text',clipboardData.getData (' text'). Replace (/ [^\ d] / GMagneur'))"
Match non-negative integers (positive integers + 0)
The copy code is as follows:
^\ dflowers $
Match positive integer
The copy code is as follows:
^ [0-9] * [1-9] [0-9] * $
Match non-positive integers (negative integers + 0)
The copy code is as follows:
^ ((-\ d+) | (0 +)) $
Match negative integer
The copy code is as follows:
^-[0-9] * [1-9] [0-9] * $
Matching integer
The copy code is as follows:
^ -?\ dbread $
Match non-negative floating point numbers (positive floating point numbers + 0)
The copy code is as follows:
^\ d + (\.\ d +)? $
Matching positive floating point number
The copy code is as follows:
^ (([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *)) $
Match non-positive floating point numbers (negative floating point numbers + 0)
The copy code is as follows:
^ ((-\ d + (\.\ d +)?) | (0 + (\ .0 +)?)) $
Match negative floating point number
The copy code is as follows:
^ (- ([0-9] +\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *)) $
Matching floating point number
The copy code is as follows:
^ (-?\ d +) (\.\ d +)? $
Matches a string of 26 English letters
The copy code is as follows:
^ [A-Za-z] + $
Matches a string of 26 English letters in uppercase
The copy code is as follows:
^ [Amurz] + $
Matches a string of 26 lowercase letters
The copy code is as follows:
^ [aMuz] + $
Matches a string of numbers and 26 English letters
The copy code is as follows:
^ [A-Za-z0-9] + $
Matches a string consisting of numbers, 26 letters, or underscores
The copy code is as follows:
^\ wit $
Match email address
The copy code is as follows:
^ [\ w -] + (\ .[\ w -] +) * @ [\ w -] + (\ .[\ w -] +) + $
Match url
The copy code is as follows:
^ [a-zA-z] +: / / match (\ w+ (-\ w+) *) (\. (\ w+ (-\ w+) *)) * (\?)? $
Match html tag
The copy code is as follows:
] *)? > (. *?)
Visual Basic & C # Regular Expression
1. Confirm valid email format
The following example uses the static Regex.IsMatch method to verify that a string is in a valid e-mail format. If the string contains a valid e-mail address, the IsValidEmail method returns true, otherwise it returns false, but takes no other action. You can use IsValidEmail to filter out e-mail addresses that contain invalid characters before the application stores the address in the database or displays it in the ASP.NET page.
[Visual Basic]
The copy code is as follows:
Function IsValidEmail (strIn As String) As Boolean
'Return true if strIn is in valid e-mail format.
Return Regex.IsMatch (strIn, ("^ ([\ w -\] +) @ (\ [[0-9] {1pr 3}\. [0-9] {1pr 3}\. [0-9] {1pr 3}\.) | (([\ w -] +\.) +) ([a-zA-Z] {2pm 4} | [0-9] {1pr 3}) (\]?) $")
End Function
[C#]
The copy code is as follows:
Bool IsValidEmail (string strIn)
{
/ / Return true if strIn is in valid e-mail format.
Return Regex.IsMatch (strIn, @ "^ ([\ w -\] +) @ (\ [[0-9] {1pr 3}\. [0-9] {1pr 3}\. [0-9] {1pr 3}\.) | (([\ w -] +\.) +) ([a-zA-Z] {2pm 4} | [0-9] {1pm 3}) (\]?) $")
}
two。 Clean up input string
The following code example uses the static Regex.Replace method to extract invalid characters from a string. You can use the CleanInput method defined here to clear potentially harmful characters entered in the text field of the form that accepts user input. CleanInput returns a string after clearing all non-alphanumeric characters except @,-(hyphen), and. (period).
[Visual Basic]
The copy code is as follows:
Function CleanInput (strIn As String) As String
'Replace invalid characters with empty strings.
Return Regex.Replace (strIn, "[^\ w\. @ -]", ")
End Function
[C#]
The copy code is as follows:
String CleanInput (string strIn)
{
/ / Replace invalid characters with empty strings.
Return Regex.Replace (strIn, @ "[^\ w\. @ -]", ")
}
3. Change date format
The following code example uses the Regex.Replace method to replace the date form of mm/dd/yy with the date form of dd-mm-yy.
[Visual Basic]
The copy code is as follows:
Function MDYToDMY (input As String) As String
Return Regex.Replace (input, _
"\ b (?\ d {1Magne2}) / (?\ d {1Magne2}) / (?\ d {2jin4})\ b", _
"${day}-${month}-${year}")
End Function
[C#]
The copy code is as follows:
String MDYToDMY (String input)
{
Return Regex.Replace (input, "\ b (?\ d {1 month 2}) / (?\ d {1 month 2}) / (?\\ d {2jue 4})\ b", "${day}-${month}-${year}")
}
Regex replacement mode
This example shows how to use named backreferences in the replacement mode of Regex.Replace. Where the replacement expression ${day} is inserted by (?) The substring captured by the group.
The Regex.Replace function is one of several static functions that allow you to operate with regular expressions without creating explicit regular expression objects. If you do not want to keep compiled regular expressions, this will bring you convenience
4. Extract URL information
The following code example uses Match.Result to extract the protocol and port number from URL. For example, "http://www.penner.cn:8080... will return" http:8080 ".
[Visual Basic]
The copy code is as follows:
Function Extension (url As String) As String
Dim r As New Regex ("^ (?\ w +): / / [^ /] +? (?:\ d +)? /", _
RegexOptions.Compiled)
Return r.Match (url) .result ("${proto} ${port}")
End Function
[C#]
The copy code is as follows:
String Extension (String url)
{
Regex r = new Regex (@ "^ (?\ w +): / / [^ /] +? (?:\ d +)? /"
RegexOptions.Compiled)
Return r.Match (url) .result ("${proto} ${port}")
}
A regular expression of a password with only letters and numbers, no less than 6 digits, and all numeric letters contain
In C #, you can use this to indicate:
The copy code is as follows:
"\ w {6} (\ w +) *"
An algorithm program that splits the path string into two parts: root directory and subdirectory. Consider that the path format is C:\ aa\ bb\ cc,\\ aa\ bb\ cc. The above path of ftp://aa.bb/cc will be split into C:\ and aa\ bb\ cc,\\ aa and\ bb\ cc, respectively. Ftp:// and aa.bb/cc are implemented as follows:
The copy code is as follows:
Var strRoot,strSub
Var regPathParse=/ ^ ([^\\ ^\ /] + [\\ /] + |\ [^\] +) (. *) $/
If (regPathParse.test (strFolder))
{
StrRoot=RegExp.$1
StrSub=RegExp.$2
}
That's all for "what are the classic Javascript regular expressions?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.