This article Yes, it is good to learn regular expressions taken from the article (http://blog.blogchina.com/article_71083.306784.html) Author: Alai (Email: A at Lai.com.cn home page: http: / / www.9499.net blog: http://blog.9cbs.net/Laily/) Keywords: Regular expression pattern matching JavaScript Summary: Collect some common regular expressions. Regular Expressions Book Author: Alai (Email: A at Lai.com.cn home page: http: //www.9499.net Blog: http://blog.9cbs.net/laily/) Keyword: Regular Expressions Pattern matching JavaScript Summary: Collect some common regular expressions. Regular expressions are used in string processing, form verification, etc., practical efficient, but it is always not sure to use it, so that I often check it online. I put some commonly used expressions here and make a memo. This post will be updated in any time. Regular expressions matching Chinese characters: [/ U4E00- / u9fa5] Match Dual-byte characters (including Chinese characters): [^ / X00- / XFF] Application: Calculate the length of the string (a double-byte character lengthometer 2, ASCII characters 1) String.Prototype.len = function () {Return this.Replace ([^ / X00- / XFF] / g, "aa"). Length;} Matching a regular expression: / N [/ s |] * / R Matches the regular expression of the HTML tag: / <(. *)>. *
| <(. *) //> / Match the regular expression of the first tail space: (^ / s *) | (/ s * $) application: JavaScript does not use the TRIM function like VBScript, we can use this expression Implementation, as follows: string.prototype.trim = function () {return this.Replace (/ (^ / s *) | (/ s * $) / g, "");} Using regular expressions Decomposition and conversion IP Address: The following is the JavaScript program that matches the IP address using the regular expression, and converts the IP address into a corresponding value: function IP2V (ip) {RE = / (/ d ) /. (/ D ) /. (/ D ) / (/ d ) / g // Match the regular expression IF (Re.Test (IP)) {RETURN Regexp. $ 1 * Math.Pow (255, 3)) Regexp. $ 2 * math.pow (255 2)) regexp. $ 3 * 255 regexp. $ 4 * 1} else {throw new error ("NOT A VALID IP Address!")}} But the above program does not need a regular expression, and directly use the SPLIT function Decomposition may be simpler, the procedure 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)) Match the regular expression of the Email address: / w ([- .] / W ) * @ / w ([-.] / W ) * /. / w ([-.] / w ) * Regular expressions in matching URL: http: // ([/ w-] /.) [/ w-] (/ [/ w - ./?%&=]*)? Using the regular expression to remove the algorithm program of repeated characters in the string: [Note: This program is incorrect, the reason is seen in this post] var s = "abacabefgeeii" var s1 = s .replace (/( (.). VAR RE = new regexp ("[" s1 "]", "g") var s2 = s.replace (re, ") Alert (S1 S2) / / The result is: ABCEFGI I originally posted a method of finding an expression on 9CBS to implement the way to remove the repeating character, and finally didn't find it, this is the simplest implementation method I can think of. The idea is to use the retrieval to remove the repeated characters, and then establish a second expression with a duplicate character, take the non-repetitive character, and the two are connected. This method may not apply for a string that requires the character sequence.
The JavaScript program that extracts the file name from the URL address using a regular expression. The following results are Page1 s = "http://www.949.net/page1.htm" s = s.replace (/(//) {0,} ([^ /.] ). * / G, "$ 2") ALERT (s) Using the regular expression to limit the text box in the text box in the web form: You can only enter Chinese with regular expression restrictions: οNKEYUP = "value = value.Replace (/ [^ / U4E00- / U9FA5] / g, '')" OnBeforePaste = "ClipboardData.SetData ('Text', ClipboardData.GetData ('Text'). Replace (/ [^ / U4E00- / u9fa5] / g, ')) "Use the regular expression limit only Enter a full-corner character: οnkeyup =" value = value.replace (/ [^ / uff00- / uffff) / g,') "" OnBeforePaste = "clipboardData.setData ( 'text', clipboardData.getData ( 'text') replace (/ [^ / uFF00- / uFFFF] / g, '').)" regular expression restricted only numbers: οnkeyup = "value = value.Replace (/ [^ / d] / g, '')" onbeforepaste = "clipboardData.setdata ('text', clipboardData.getdata ('text'). Replace (/ [^ / d] / g , '') "Use regular expression to limit only numbers and English: οnkeyup =" value = value.replace (/ [/ w] / g, '') "OnBeforePaste =" ClipboardData.SetData ('Text', ClipboardData.getdata ('text'). Replace (/ [^ / d] / g, '') "Regular expression, related link http://blog.9cbs.net/laily/category/19548.aspx http: //BLOG.9CBS.NET/LAILY/Archive/2004/06/30525.ASPX Microsoft's regular expression tutorial (5): Select / Group and backward reference http://blog.9cbs.net/Laily/ Archiv E / 2004/06/30 / 30522.ASPX Microsoft's Regular Expression Tutorial (4): Limits and Locator http://blog.9cbs.net/LAILY/Archive/2004/06/30517.aspx Microsoft Regular expression tutorial (3): Character match http://blog.9cbs.net/LAILY/Archive/2004/06/30514.ASPX Microsoft's regular expression tutorial (2): Regular expression syntax and priority Rights http://blog.9cbs.net/Laily/archive/2004/06/30511.ASPX Microsoft's regular expression tutorial (1):
Regular expression Introduction http://blog.9cbs.net/LAILY/Archive/2004/06/30/30360.aspx applet Assembly: Advanced Find / Replace, Regular Expression Exercise, JavaScript Script Program Detector HTTP: //BLOG.9CBS.NET/LAILY/Archive/2004/06/24/25872.aspx classic regular expression regular expression, regular expression, regular expression matching, regular expression syntax, pattern match, regular expression matching JavaScript regular expression ASP regular expression ASP.NET regular expression C # regular expression JSP regular expression PHP regular expression VB.NET regular expression VBScript regular expression programming Delphi regular expression JScript regular expression Regular Expression regular expression Regexp mode Pattern matches Match .NET namespace: system.text.regulaRexpression supplement: ^ / d $ // Match non-negative integer (positive integer 0) ^ [0-9] * [1-9] [0-9] * $ // Matching positive integers ^ ((- / d ) | (0 )) $ // Match unusual integer (negative integer 0) ^ - [0-9] * [1-9] [0-9 ] * $ // Matching negative integers ^ -? / D $ // Match integer ^ / d (/. / D )? $ // Match the number of non-looted points (positive floating point 0) ^ (([0-9 ] /. [0-9] * [1-9] *) | ([0-9] * [1-9] [0-9] * /. [0-9] ) | ([0-9] * [1-9] [0-9] *)) $ // Match positive floating point number ^ ((- / d (/ d )?) | (0 (/. 0 )?)) $ // Match non-positive floating point numbers (load point 0) ^ (([([0-9] /. [0-9] * [1-9] [0-9] * ) | ([0-9] * [1-9] * /. [0-9] ) | ([0-9] * [1-9] [0-9] *)) ) $ // Match the pocket number ^ (-? / D ) (/. / D )? $ // Match the floating point number ^ [A-ZA-Z] $ // Match the string consisting of 26 English letters ^ [AZ] $ // Match the string of the uppercase of 26 English letters ^ [AZ] $ // Match the string of lowercase consisting of 26 English letters ^ [A-ZA-Z0-9] $ // Match string consisting of numbers and 26 English letters ^ / w $ // Matching strings consisting of numbers, 26 English letters, or underscore ^ [/ w -] (/ w -] ) * @ [/ w -] (/. [/ W-] ) $ // Match Email Address ^ [A-ZA-Z] : // Match (/W (-/W ) * (/. (/w (-/w ) * * (/? / S *)? $ // Match the URL to use the regular expression to remove the repeated character of the repetition character: var s = "abacabefgeeii" var s1 = s.Replace (/(( (.).. / 1 / g, " $ 1 ") VAR RE = New RegeXp (" [" S1 "] "," G ") var s2 = s.Replace (re," ") ALERT (S1 S2) / / result is: ABCEFGI ==== ==============
============= If var s = "abacabefggeeii" is wrong, the result is: ABEICFGGGG regular expression ability limited RE: TOTORO thank you, this JavaScript regular expression program algorithm There is really a problem, I will try to find a better way!!! 1. Confirm that the code example below the effective email format Use the static regex.ismatch method to verify that a string is a valid email format. If the string contains a valid email address, the IsValideMail method returns true, otherwise returns false, but no other operations are taken. You can use IsValidemail to filter an email address that contains an invalid character before the application stores the address in the database or appears in the ASP.NET page. [Visual Basic] Function isvalideMail (STRIN As String) AS Boolean 'Return True IF STRIN IS in Valid E-mail Format. Return Regex.ismatch (Strin ("^ (" ^ ([/ w - /.] ) @ (/ [[0-9] {1,3} /. [0-9] {1,3} /. [0-9] {1,3} /.) | ([/ W -] /.) )) ([A-ZA-Z] {2, 4} | [0-9] {1,3}) (/]?) $ ") end function [c #] bool isvalidemail (string string) {////// Return True if strin is in valid e-mail format. Return Regex.ismatch (Strin, @ "^ ([/ w - /.] ) @ (/ [[0-9] {1, 3} /. 0-9] {1,3} /. [0-9] {1,3} /.) | ([/ W -] /.) ))) ([A-ZA-Z] {2, 4} | [0-9] {1, 3}) (/]?) $ ");} 2. Clean the code example below the Cleanup String Using the Static Regex.Replace method to extract an invalid character from the string. You can Using the CleanInput method defined here, remove the possible harmful characters entered in the text field that accepts the user-entered form. CleanInput is cleared @, - (even character) and. (The period) all non-alphabetical numbers A string is returned after the character. [Visual Basic] Function CleanInput (Strin As String) AS String 'Replace Invalid Characters with Empty Strings. Return Regex.replace (Strin, "[^ / W /.@-]", "" " End function [c #] string cleanInput (string string) {// replace invalid characterid characters with empty strings. Return regex.replace (Strin, @ "[^ / w /.@-]", "");} 3. Change date Format The following code example uses the regex.replace method to use DD-MM-YY Date form instead of the date form of MM / DD / YY. [Visual Basic] Function MDYTODMY (INPUT AS STRING) AS STRING RETURN Regex.replace (Input, _ "/ B (? / D {1, 2}) / (?
/ D {1, 2}) / (?
/ D {2,4}) / b ", _" $ {day} - $ {MONTH} - $ {year} ") End function [c #] string mdytodmy (string infut) {Return Regex.replace (Input," // b (? // d {1, 2}) / (?
// D {1, 2}) / (?
// D {2, 4}) // b "," $ {day} - $ {month} - $ {year} ");} Regex Replacement mode This example shows how to use naming in regex.replace replacement mode Reverse reference. Where, replacement expression $ {day} insert (?
...) Group capture subtrings. There are several static functions that allow you to create an explicit regular expression object when using regular expressions, while the regex.replace function is one of them. If you don't want to keep compilation regular expressions, this will bring you convenient 4. Extract URL information The following code example uses Match.Result to extract protocols and port numbers from the URL. For example, "http://www.contoso.com:8080/letters/readme.html" will return "HTTP: 8080". [Visual Basic] Function Extension (URL AS STRING) AS STRING DIM R AS New Regex ("^ (?
/ w ): // [^ /] ? (?
: / d )? / ", _ regexoptions.compiled) Return R.Match (URL) .result (" $ {pro} $ {port}) End Function [C #] string extension (String URL) {regex r = new Regex (@ "^ (?
/ w ): // [^ /] ? (?
: / d )? / ", regexoptions.compiled; return r.match (URL) .result (" $ {Proto} $ {port});} Today, some netizens ask: How to use regular expressions either digital Either a letter is a letter, can only be a letter number, it doesn't matter? My answer is: ^ [A-ZA-Z] $ | ^ / D $