JS matching date and time regular expression example

The instance of this paper describes the regular expression of JS matching date and time. Share to everyone for reference, specifically as follows:

// Date Regular expression var REG = / ^ [1-9] \ d {3} – (0 [1-9] | 1 [0-2]) – (0 [1-9] | [1-2] [0-9] | 3 [0-1]) $ /; var regexp = new regexp (REG ); if (! regexp.test (value)) {alert (“The date format is incorrect, the correct format is: 2014-01-01”); return;} // Time regular expression var REG = / ^ (20 | 21 | 22 | 23 | [0-1] \ d): [0-5] \ D: [0-5] \ D $ /; var regexp = new regexp (reg); if (!regxp.test) Value)) {Alert (“The time format is incorrect, the correct format is: 12: 00”); return;} // Date + time regular expression var REG = / ^ [1-9] \ d {3 } – (0 [1-9] | 1 [0-2]) – (0 [1-9] | [1-2] [0-9] | 3 [0-1]) \ S + (20 | 21 | 22 | 23 | [0-1] \ D): [0-5] \ D: [0-5] \ D $ /; var regexp = new regexp (reg); if (!regxp.test (value) {Alert (“The time format is incorrect, the correct format is: 2014-01-01 12:00:00”); return;}});
   
Ps: Here you will provide you with 2 very convenient regular expression tools for your reference:

JavaScript regular expression online test tool:

http: //Tools.jB51.Net/regex/javascript
Regular expression online generation tool:

http://tools.jb51.net/regex/create_reg
More Readers who are interested in JavaScript can view this topic: “JavaScript regular expression skills”, “JavaScript Replace Skill Summary”, “JavaScript Find Algorithm Skills Summary”, “JavaScript Data Structure and Algorithm Skills Summary”, ” JavaScript Travel Algorithm and Skill Summary ” help.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment