Classic regular expression

xiaoxiao2021-03-05  25

Classic Regular Expression Keyword: Regular Expression Mode 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 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.

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

New Post(0)