Several examples of regular expressions in JavaScript

xiaoxiao2021-03-06  105

Several examples of regular expressions in JavaScript 1 (transfer)

Remove the processing of both ends of the string

If you use a traditional way, it is possible to use the following manner to clear the left space Function JS_LTRIM (Deststr) {if (deststr == Null) Return "; var pos = 0; var retrstr = new String (Deststr ); If (retstr.lenght == 0) Return Retstr; while (retstr.substring (pOS, POS 1) == "") POS ; RetStr = RetStr.Substring (POS); return (Retstr);} // Clear the right space FUNCTION JS_RTRIM (DESTSTR) {if (deststr == null) Return ""; var rettstr = new string (deststr); var pos = retrstr.length; if (pOS == 0) Return RetStr; WHILE (POS && Retstr.Substring (POS-1, POS) == "") POS -; retstr = retrstr.substring (0, pOS); return (RETSTR);} // Clear left and right space Function JS_TRIM (Deststr) {IF (deststr == null) Return "" "" ""; var rettstr = new string (deststr); var pOS = retstr.length; if (pOS == 0) Return RetStr; RetStr = js_ltrim (RetStr); RetStr = js_rtrim (Retstr); Return Retstr;}

Using regular expressions to remove spaces on both sides, simply need the following code string.prototype.trim = function () {Return this.Replace (/ (^ / s *) / g, "") }

It is done in one sentence, and the regular expression saves us quite written code.

! Mobile phone number check

If the traditional check method is used to complete the verification of the following three steps, (1). Is it a number (2). Is it a 11-bit (3). The third bit of the number is 5, 6, 7, 8.9 If you use a regular expression check, you only need the following code Function CheckMobile1 (Form) {if (Form.Mobile.Value> "") {var REG = / 13 [5, 6, 7, 8, 9] / D {8} /; ife (reg) == null) {Alert ("Please enter the correct mobile phone number!"); form.mobile.focus (); return false;}} Return True;}

From the above code, you can see that the mobile phone number only needs to define a var REG = / 13 [5, 6, 7, 8, 9] / d {8} /; mode matching strings can complete the legality check.

! URL's check, condition: must begin with http: // or https: //, the port number must be between 1-65535, the following code completes the legitimacy check

// OBJ: Data Objects // Dispstr: Failed Tips Display String Function CheckURLVALID (OBJ, DISPSTR) {IF (Obj == NULL) {Alert ("Incoming Object is empty); return false;} var str = Obj.value;

Var urlpatern0 = /^ # i (! URLPATERN0.TEST (STR)) {Alert (Dispstr "is illegal: must be with 'http:' or 'https:' beginning!"); OBJ. FOCUS (); return false;} var urlpatern2 = /^https?: (([a-za-z0-9_-]) (/d ) * (:/d )?? @ i; if (! URLPATERN2.TEST (STR)) {Alert (DISPSTR "port number must be numbers and should be between 1-65535!"); obj.focus (); return false;}

VAR URLPATERN1 = / ^ https?: (([A-ZA-Z0-9 _-]) (/.)?) * (/ D )? (// ((/.)? (/?)? = ? &? [A-ZA-Z0-9 _-] (/?)?) *) * $ / i;

If (! urlpatern1.test (str)) {Alert (Dispstr "is not legal, please check!"); obj.focus (); return false;}

VAR S = "0"; var t = 0; var RE = new regexp (": // D ", "ig"); while ((arr = re.exec (str))! = null) {s = str .substring (regexp.index 1, regexp.lastindex);

IF (s.SUBSTRING (0, 1) == "0") {Alert (DISPSTR "port number cannot be started with 0!"); obj.focus (); return false;}

T = parseint (s); if (t <1 || t> 65535) {Alert (DISPSTR "port number must be a number and should be between 1-65535!"); obj.focus (); Return False;} } Return true;} The verification of the URL, it seems to have a lot of code, because it is necessary to give an error message, otherwise only var buringpatern1 = / ^ https?: (([A-ZA-Z0-9_-]) (/.)?) * (/ d )? (// ((/.)? (/?)? =? &? [A-ZA-Z0-9 _-] (/?)?) *) * $ / i; one sentence can verify the legality of the URL

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

New Post(0)