Regular expression pattern match 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 double-byte characters (including Chinese characters): [^ / X00- / XFF]
Application: Calculate the length of the string (a double-byte character length 2, ASCII characters 1)
String.prototype.len = function () {return this.Replace ([^ / X00- / XFF] / g, "aa"). Length;}
Regular expression of matching blank line: / n [/ s |] * / r
Matching the regular expression of HTML tag: / <(. *)>. * // 1> | <(. *) //> /
Regular expressions matching the first tail space: (^ / s *) | (/ s * $)
Application: There is no TRIM function like VBScript in JavaScript, we can use this expression to be implemented, as follows:
String.prototype.trim = function () {Return this.Replace (/ (^ / s *) | (/ s * $) / g, "");
Using regular expressions to decompose and convert IP addresses:
Below is the JavaScript program that uses the regular expression to match the IP address and convert the IP address into the corresponding value:
Function IP2V (IP) {RE = / (/ d ) /. (/ d ) /. (/ d ) /. (/ d ) / g // Match the regular expression IF of the IP address (Re.Test (IP) $ 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! ")}}
However, the above program does not use a regular expression, and directly use the SPLIT function to decompose may be simpler, the program 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 ) *
Matching URL URL regular expressions: http: // ([/ w-] /.) [/ W-] (/ [/ w- ./?%&=]*)?
Algorithm program that uses regular expressions to remove repeated characters in strings
: [Note: This procedure is incorrect, the reason is seen in this Post Reply]
Var s = "AbacaFgeeii" var s1 = s.Replace (/( (.). "$ 1") VAR RE = New Regexp ("[" S1 "]", "G") VAR S2 = S.Replace (re, ") ALERT (S1 S2) / / The result is: Abcefgi I originally posted a way to find an expression on 9CBS to implement the method of removing the repeating character, and I didn't find it, this is what I can think of. The simplest implementation method. 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. You have to extract the JavaScript program of the file name from the URL address using a regular expression. The following results are page1
S = "http://www.9499.net/page1.htm" s = s.Replace (/ (//) {0,} ([^ /.] ). * / ig, "$ 2" Alert (s)
Use the regular expression to limit the text box in the text box in the web form:
Use the regular expression restrictions can only be entered in Chinese: οNKEYUP = "value = value.replace (/ [^ / U4E00- / U9FA5] / g, '')" onbeforepaste = "ClipboardData.SetData ('Text', ClipboardData.Getdata) 'Text'). Replace (/ [^ / U4E00- / U9FA5] / g, '')) "
Use the regular expression to limit only full-width characters: οnkeyup = "value = value.replace (/ [^ / uff00- / uffff) / g, ')" onbeforepaste = "clipboardData.setdata (' Text ', ClipboardData.Getdata) ('text'). Replace (/ [^ / UFF00- / UFFFFF] / g, '')) "
Use regular expressions to limit only numbers: οnkeyup = "value = value.replace (/ [^ / d] / g, '')" onbeforepaste = "ClipboardData.SetData ('Text', ClipboardData.Getdata ('Text' ) .Replace (/ [^ / d] / g, ')) ")
Use regular expressions to limit only numbers and English: οNKEYUP = "value = value.replace (/ [/ w] / g, '')" onbeforepaste = "ClipboardData.SetData ('Text', ClipboardData.Getdata ('Text ') .Replace (/ [^ / d] / g,'))
Copyright Notice: 9CBS is this BLOG managed service provider. If this paper involves copyright issues, 9CBS does not assume relevant responsibilities, please contact the copyright owner directly with the article Author.
Posted on June 24, 2004 11:32 PM
Feedback # Reply: Classic Regular Expression 2004-08-03 2:03 PM Alai.com
Regular expression, related links http://blog.9cbs.net/Laily/category/19548.aspx http://blog.9cbs.net/Laily/archive/2004/06/30/30525.ASPX Microsoft's regular expression Tutorial (5): Select / Group and Back to reference http://blog.9cbs.net/LAILY/Archive/2004/06/30522.ASPX Microsoft's regular expression tutorial (4): Limits and positioning Http://blog.9cbs.net/Laily/archive/2004/06/30517.ASPX Microsoft's regular expression tutorial (3): Character match http://blog.9cbs.net/ilar/archive/ 2004/06/30 / 30514.ASPX Microsoft's Regular Expression Tutorial (2): Regular Expression Syntax and Priority Order 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 small program As: Advanced Find / Replace, Regular Expression Exercise, JavaScript script programs http://blog.9cbs.net/LAILY/Archive/2004/06/24/25872.aspx classic regular expressions regular expressions, regular expressions, regular expressions match, regular expression Grammar, pattern matching, 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 program Delphi regular expression JScript # Reply: Classic Regular Expression 2004-08-03 2:12 PM Ara
Regular expressions Regular Expression Regular Expression Regexp Mode Pattern matches Match .NET namespace: system.text.RegularExpression
# 回:: Classic regular expression 2004-08-03 2:14 PM Ara
Supplement: ^ / d $ // Match non-negative integer (positive integer 0) ^ [0-9] * [1-9] [0-9] * $ // Match positive integer ^ ((- / d ) | (0 )) $ // Matching the 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] *) | ([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 number (negative Floating point 0) ^ ((((([0-9] /. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [ 0-9] * /. [0-9] ) | ([0-9] * [1-9] [0-9] *)))) $ // Matching the load point number ^ (-? / D ) (/./d )?$ // Match floating point ^ [A-ZA-Z] $ // Match string from 26 English letters ^ [AZ] $ // Match by 26 English letters String ^ [AZ] $ // composed of uppercases, matching strings consisting of 26 English letters, composed of characters composed of numbers and 26 English letters String ^ / w $ // Matching strings made of numbers, 26 English letters or underscore ^ [/ w -] (/. [/ W -] ) * @ [/ w -] (/. / w -] ) $ // Match Email Address ^ [A-ZA-Z] : // Match (/W (-/W ) * (/.(/w (-/w ) *) * (/? / S *)? $ // Match URL # Reply: Classic Regular Expression 2004-09-08 7:37 PM Totoro
Using Regular Expression Removal Binding A Duplicate Character Algorithm: Var S = "AbacaBeeii" Var S1 = S.REPLACE (/( (.).) VAR RE = New Regexp (" [" S1 "] "," g ") var s2 = s.Replace (re," ") ALERT (S1 S2) / / The result is: ABCEFGI ============== ================= If var s = "abacabefggeeii" is wrong, the result is: ABEICFGG regular expression ability
# 回:: Classic regular expression 2004-09-10 2:07 PM Ara
Re: Totoro Thank you, this JavaScript regular expression program algorithm has no problem, I will try to find a better way !!!
# 回 回: Classic regular expression 2004-10-11 3:52 PM LAI
1. Verify 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 (?
Today, there are netizens asked: How to use regular expressions to indicate either number or letters are letters, can only be an alphanumeric number? My answer is: ^ [a-za-z] $ | ^ / d $
# 正 则 Expression Daquan 2004-10-19 3:01 PM HD
Ping Back from: blog.9cbs.net
# 回: Classic regular expression 2004-10-20 10:20 AM regular expression Remove strings Duplicate characters
I have two ways to have a function of repeated characters in the regular expression to remove strings.
# 回: Classic regular expression 2004-10-21 5:30 PM bigd
Only letters and numbers, not less than 6 digits, and how to write the regular expression of the password included in the digital letters? Thank you! Please reply
# 经 经 正 表 表 2004-10-22 4:31 PM NAIVE1010
Ping Back from: blog.9cbs.net
# 回 经: Classic regular expression 2004-10-29 5:07 PM Alai.comre bigd, this is more difficult!
# 回:: Classic regular expression 2004-11-01 5:34 PM Jackeylaw
To bired: Digital or letters exactly match more than 6 times or more ~~~
# 回:: Classic Regular Expression 2004-11-02 12:05 PM Forest
I have a TextBox control in C #, you can enter Chinese characters, punctual symbols, or letters, want to use a RegularExpressionValidator control to perform length verification, how to write validationExpress?
# 回:: Classic regular expression 2004-11-06 12:55 PM blog small white
To bired: 1. Exclusion All the six-digit string and 2. exclude all the six-digit string and 3. Arbitrary numbers and arbitrary alphabetical combinations of arbitrary alphabets. If you don't know if there is a logic of the regular style, if not, I can only match the three match, first matching condition 3, match the condition 1, and then match the condition 2, this logic can be optimized, but it should be Determine whether the password is in line with the format you need. Do you pay attention to see logical instructions. Arai.com's logical thinking is quite strong, worth admire!
# 回 回: Classic regular expression 2004-11-07 5:55 PM chubby
How to judge double quotes
# 回:: Classic regular expression 2004-11-09 6:08 PM Ai
Match [and all the characters behind it, characters include numbers, parentheses, English case and Chinese ~~
# 回:: Classic regular expression 2004-11-14 9:04 PM GK14
To Bird only, how to write the regular expression of the password containing the password included in the number, and the number alphabetic contains? Thank you! Please reply ---------------------------------- in C #, you can use this: "/ w { 6} (/ w ) * "
# 回 回: Classic regular expression 2004-11-14 9:10 PM GK14
By the way, who knows how to match the date? 2004/03/11 2004-03-11 20040311 can be more difficult if there is also a leap year. Is it better to be better in the server?
# 回 回: Classic Regular Expression 2004-11-28 6:23 PM Blue1000
I have a problem (ASP), help solve the regular expression, I want to deny a string such as: "", but if I use [^ / a>], it is negative " <"Or" / "or" a "or"> "rather than" "I want to write this match now?
# Regular expressions, related links 2004-11-30 9:35 am LONZ
Ping Back from: blog.9cbs.net
# 回: Classic regular expression 2004-12-02 4:40 PM Clamphammer
^ / D (/. / d )? $ Non-looted point number above this expression seems to be a bit problem, you enter 131A213 Try a try
# 回: Classic regular expression 2004-12-11 4:54 PM seals
Ask a 0.1 integer multiple, how to use it,
# 回:: Classic regular expression 2004-12-14 3:05 PM Arai.com
RE Blue1000: Don't you use if NOT in ASP? Re Clamphammer: No problem Rea Leopard: This question is not the best solution, use language functions to do better # Reply: Classic regular expression 2004-12-17 2:19 PM ACME
Yes, thank you master
# 回:: Classic regular expression 2004-12-24 10:33 AM ALIL.COM
Today I wrote a algorithm program that will need to split the path stroke into root directory and subdirectory, consider the path formats: C: / AA / BB / CC, // aa / bb / cc, ftp: // aa The .bb / cc The above path will be splitted to: C: / and AA / BB / CC, // AA and / BB / CC, FTP: / / and AA.BB/CC, using JavaScript as follows: Var strroot STRSUB VAR regPARSE = / ^ ([^ // ^ //] [] | [^ //] ) (. *) $ / if (regPathParse.test (strroot = regexp. $ 1 strsub = Regexp. $ 2}
# 回:: Classic Regular Expression 2004-12-27 10:14 PM NetProxy
HTML TAG is not very correct, correct for s * (/ s ) (/ s [^>] *)?> (. *?) s * //// 1 / s *>