Check the regular expression

zhaozj2021-02-16  45

Regular expression of the verification function is much stronger than usual. It can simply test whether the input value is empty, or check if the input IP address is correct. Microsoft's instructions for regular expressions are extremely simple and vague. This article will explain how the regular expressions are explained by example, which describes how to replace these string functions. Regular expressions are a very useful tool!

I will explain the application of the regular expression by two examples, which can be written in VBScript and JavaScript. The first example is a familiar regular expression check, and the second example shows how to verify the IP address.

Calibration Example This example describes how to simply verify that the input data is empty.

Client JavaScript check code: 01: 02: 03: Validation Example </ title> 04: <script language = "javascript"> 05: // If the input value is empty, the function returns False06: Function CheckenteredSomething (StringToCheck, FieldName) {07: Var RespaceCheck = / ^ / s * $ /; // Create a regular expression object 08:09: if (respaceCheck.test (StringToCheck)) {10: alert ('you Must Enter Something Into The ' FieldName ' Field. '); 11: Return False; // Input Error 12:} 13: 14: Return True; // Input Corrected 15:} 16: </ Script> 17: < / head> 18: <body> 19: <form name = "testform" id = "testform" method = "post" action = "" 20: οnsubmit = "Return (CheckenteredSomething (window.document.testform.username.Value, 'name'); "> 21: <center> 22: What is you name? <input type =" text "name =" username "id =" Username "value =" /> <br /> 23: < Input type = "submit" name = "submitbutton" id = "submitbutton" value = "enter" /> 24: </ center> 25: </ form> 26: </ body> 27: </ html></p> <p>It can be seen that the regular expression check is not difficult to use JavaScript. At Chapter 7, create a regular expression object with a slash (/). Two slashs are the regular expression type. "^" Is the string start, "/ s" represents space (Space) and jumping (Tab), "*" is a wildcard, and "$" is the end of the string.</p> <p>The 9th line is the detection method of the regular expression, and the string of the test input matches the regular expression. If the test is true, the input value is empty. I put the check process in a function, you can save it as a file, wherever you need to enter a verified page can be used. So what about a browser that does not support JavaScript, a personal electronic assistant (PDA), how to do mobile phones? You can write code with VBScript of the server ASP page.</p> <p>Server VBScript check code: 01: <% @ language = vbscript02: enablesessionState = false%> 03: <! - test.asp file -> 04: <% 05: DIM UserName06: Dim RespaceCheck07: Dim FormPost08: DIM failedTest09: 00: if Request.Form.Item ( "submitButton") = "Enter" then11: formPost = true12: else13: formPost = false14: end if15: 16: if formPost then17: username = Request.Form.Item ( "username ") 18: set respacecheck = new regexp19: RespaceCheck.pattern =" ^ / s * $ "20:21: failedtest = RespaceCheck.test (username) 22: End iF23:%> 24: <html> 25: <head> 26: <title> Validation Example </ title> 27: </ head> 28: <body> 29: <center> 30: <% if FormPost Then31: if FailedTest Then%> 32: <b> <font color = " Red "> You Must Enter Something Into The Name Field. </ font> </ b> 33: <% else%> 34: <b> hello <% = username%> </ b> 35: <% end if %> 36: <br /> 37: <% end if%> 38: <form name = "testform" id = "testform" method = "post" action = "test.asp"> 39: What is you name? <input type = "text" name = "username" id = "username" value = "/> <br /> 40: <input type =" submit "name =" Submitbutton "ID =" Submi TButton "Value =" Enter "/> 41: </ form> 42: </ center> 43: </ body> 21: </ html></p> <p>There is not much difference between the two. Creating a regular expression object in line 18, 19 rows set the regular expression type, and then compare the discrimination. Matching examples now illustrate more complex checks, such as IP addresses.</p> <p>Client JavaScript matching check code: 01: <html> 02: <head> 03: <title> match example </ title> 04: <script language = "javascript"> 05: // If the IP address is illegal, return false06 : Function checkaddress (ipaddressstring) {07: var respacecheck = / ^(/d )/.(/d )/. (/d ) / (聽) $$/;08: var passedtest = false; 09: 10: IF (ipaddressstring) {11: ipaddressstring.match (RespaceCheck); 12: IF (regexp. $ 1 <= 255 && regexp. $ 1> = 0 13: && regexp. $ 2 <= 255 && regexp. $ 2> = 0 14: && Regexp. $ 3 <= 0 15: && Regexp. $ 4 <= 255 &®Exp. $ 4> = 0) {16: PassedTest = true; 17:} 18:} 19: 20: if (! Passedtest) {21: Alert ('You Must Enter a Valid IP Address.'); 22: Return False; 23:} 24: 25: Return True; 26:} 27: </ script> 28: </ head> 29: <body> 30: <form name = "testform" id = "testform" method = "post" action = "" 31: οnsubmit = "return (checkaddress (window.document.testform.ipaddress.value) )); "> 32: <center> 33: Enter an ip address: <input type =" text "name =" ipaddress "id =" ipaddress "V Alue = "" /> <Br /> 34: <input type = "submit" name = "submitbutton" id = "submitbutton" value = "enter" /> 35: </ center> 36: </ form> 37: </ body> 38: </ html></p> <p>The 7th line code creates a regular expression object. But the type of this expression is different. The parameters in parentheses () are used for subsequent subsections. "/ d" means that digital matching, "/ d " requires at least one number. After the test input value matches, the sub-test determines that the numbers of each section are between 0 and 255. When the sub-test uses the global object regexp, it automatically generates when the string.match method is called. Use VBScript to write some code, but you can achieve the same function.</p> <p>Service VBScript matching check code: 01: <% @ language = vbscript02: enablesessionState = false%> 03: <! - test2.asp file -> 04: <% 05: DIM iPaddress06: Dim ReipaddressCheck07: Dim FormPost08: Dim passedTest09: Dim Matches00: Dim num111: Dim num212: Dim num313: Dim num414: 15: if Request.Form.Item ( "submitButton") = "Enter" then16: formPost = true17: else18: formPost = false19: end if20: 21: IPADRESS = Request.form.Item ("ipaddress" 23: set respaceCheck = new regexp24: respaceCheck.pattern = "^ (/ d ) /. (/ D ) /. (/ D ) /. (/ d ) $ "25:26: if respacecheck.test (ipaddress) Then27: set matches = respternation.execute (ipaddress) 28: Num1 = matches.Item (0). Submatches.Item (0) 29: Num2 = matches .item (0). Submatches.Item (1) 30: Num3 = matches.Item (0). Submatches.Item (2) 31: Num4 = matches.Item (0). Submatches.Item (3) 32: if Num1 <= 255 and Num1> = 0 _33: And Num2 <= 255 and num2> = 0 _34: and num 3 <= 255 and num 3> = 0 _35: and Num4 <= 255 and Num4> = 0 THEN36: PassedTest = true37: END IF38: END IF39: END IF40:%> 41: <HTM L> 42: <head> 43: <title> match example </ title> 44: </ head> 45: <body> 46: <center> 47: <% if FormPost Then48: if passedTest Then%> 49: < B> You Entered <% = ipaddress%> As a valid ip address. </ b> 50: <% else%> 51: <b> <font color = "red"> You Must Enter a Valid IP address. </ FONT> </ b> 52: <% end if%> 53: <br /> 54: <% end if%> 55: <form name = "testform" id = "testform" method = "post" action = " Test2.asp</p> <p>> 56: Enter an ip address "name =" ipaddress "id =" ipaddress "value =" "/> <br /> <br> 57: <input type =" submit "name =" subsmitbutton "ID = "Submitbutton" value = "Enter" /> 58: </ form> 59: </ center> 60: </ body> 61: </ html> Unlike JavaScript, VBScript does not have a match command. So use the regular expression of the Execute method to handle the matching test. The EXECUTE returns include four sub-arrays, corresponding to the detection result of each number. Then detected whether the number is in the range of 0-255. Detecting correct IP addresses.</p> <p>Through this article, it is desirable to help initially understand the usage of regular expressions. You can download the script help file in Microsoft Site.</p> <p>Happy programming!</p> <p>JS Chinese Help http://www.chinaok.net/down/jscript5.zip</p> <p>VBS Chinese Help http://www.chinaok.net/down/vbscript.zip</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-27866.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="27866" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.042</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'ABFDC8fB31H4ehhOd2w5Zs73GeB7if8gKadttnZAXNwfypOTCzDpv2fHMr8me5TQ9Xn6QU_2Bm_2BaSbhcajwXfl4w_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>