Common matching regular expression and instance (Updating)

xiaoxiao2021-03-06  99

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 Match the regular expression of the HTML tag: / <(*)>. * | <(*) //> / match the regular expression of 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 Expression Decomposition and Convert IP Address: The following is to match the IP address using regular expressions, and convert the IP address into Corresponding value JavaScript program: Function IP2V (IP) {RE = / (/ d ) /. (/ D ) /. (/ D ) /. (/ D ) / g // matches the regular expression IF of the IP address ( 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!")}} However, if the above programs do not need a regular expression, it may be more simple to use the SPLIT function to decompose, 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 ) * matching URL URL regular expression: http: // ([/ w-] /.) [/ W-] (/ [/ w- ./?%&=]*)? Using regular expressions to remove strings Algorithm for repeated characters: [Note: This program is incorrect, see this post reply] var s = "abacabefgeeii" var s1 = S.Replace (/( (.) VAR RE = New 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 repeating characters, which is not found, this is the simplest implementation 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.

转载请注明原文地址:https://www.9cbs.com/read-95985.html

New Post(0)