Created
October 31, 2017 02:17
-
-
Save muhfaris/17b0649bd3308a088b0a372a2fe0f515 to your computer and use it in GitHub Desktop.
Pattern regular expression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Scape slashes is simply use \ before / and it will be escaped. (\/=> /). | |
Otherwise you're regex DD/MM/YYYY could be next: | |
/^[0-9]{2}[\/]{1}[0-9]{2}[\/]{1}[0-9]{4}$/g | |
Explanation: | |
[0-9]: Just Numbers | |
{2} or {4}: Length 2 or 4. You could do {2,4} as well to length between two numbers (2 and 4 in this case) | |
[\/]: Character / | |
g : Global -- Or m: Multiline (Optional, see your requeriments) | |
$: Anchor to end of string. (Optional, see your requeriments) | |
^: Start of string. (Optional, see your requeriments) | |
An example of use: | |
var regex = /^[0-9]{2}[\/][0-9]{2}[\/][0-9]{4}$/g; | |
var dates = ["2009-10-09", "2009.10.09", "2009/10/09", "200910-09", "1990/10/09", | |
"2016/0/09", "2017/10/09", "2016/09/09", "20/09/2016", "21/09/2016", "22/09/2016", | |
"23/09/2016", "19/09/2016", "18/09/2016", "25/09/2016", "21/09/2018"]; | |
//Iterate array | |
dates.forEach( | |
function(date){ | |
console.log(date + " matches with regex?"); | |
console.log(regex.test(date)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment