|
| 1 | + |
| 2 | +### Commonly used Regex |
| 3 | + |
| 4 | +#### 1\. Digits |
| 5 | + |
| 6 | +* [Whole Numbers](https://www.regexpal.com/?fam=104020) – `/^\d+$/` |
| 7 | +* [Decimal Numbers](https://www.regexpal.com/?fam=104021) – `/^\d*\.\d+$/` |
| 8 | +* [Whole + Decimal Numbers](https://www.regexpal.com/?fam=104022) – `/^\d*(\.\d+)?$/` |
| 9 | +* [Negative, Positive Whole + Decimal Numbers](https://www.regexpal.com/?fam=104023) – `/^-?\d*(\.\d+)?$/` |
| 10 | +* [Whole + Decimal + Fractions](https://www.regexpal.com/94462) – `/[-]?[0-9]+[,.]?[0-9]*([\/][0-9]+[,.]?[0-9]*)*/` |
| 11 | + |
| 12 | +#### 2\. Alphanumeric Characters |
| 13 | + |
| 14 | +* [Alphanumeric without space](https://www.regexpal.com/?fam=104024) – `/^[a-zA-Z0-9]*$/` |
| 15 | +* [Alphanumeric with space](https://www.regexpal.com/?fam=104025) – `/^[a-zA-Z0-9 ]*$/` |
| 16 | + |
| 17 | +#### 3\. Email |
| 18 | + |
| 19 | +* [Common email Ids](https://www.regexpal.com/?fam=104026) – `/^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$/` |
| 20 | +* [Uncommon email ids](https://www.regexpal.com/?fam=104027) – `/^([a-z0-9_\.\+-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/` |
| 21 | + |
| 22 | +#### 4\. Password Strength |
| 23 | + |
| 24 | +* [Complex](https://www.regexpal.com/?fam=104028): Should have 1 lowercase letter, 1 uppercase letter, 1 number, 1 special character and be at least 8 characters long |
| 25 | + |
| 26 | + /(?=(.\*\[0-9\]))(?=.\*\[\\!@#$%^&\*()\\\\\[\\\]{}\\-\_+=~\`|:;"'<>,./?\])(?=.\*\[a-z\])(?=(.\*\[A-Z\]))(?=(.\*)).{8,}/ |
| 27 | + |
| 28 | +* [Moderate](https://www.regexpal.com/?fam=104029): Should have 1 lowercase letter, 1 uppercase letter, 1 number, and be at least 8 characters long |
| 29 | + |
| 30 | + /(?=(.\*\[0-9\]))((?=.\*\[A-Za-z0-9\])(?=.\*\[A-Z\])(?=.\*\[a-z\]))^.{8,}$/ |
| 31 | + |
| 32 | +#### 5\. Username |
| 33 | + |
| 34 | +* [Alphanumeric string](https://www.regexpal.com/?fam=104030) that may include \_ and – having a length of 3 to 16 characters – `/^[a-z0-9_-]{3,16}$/` |
| 35 | + |
| 36 | +#### 6\. URL |
| 37 | + |
| 38 | +* Include [http(s) Protocol](https://www.regexpal.com/?fam=104034) |
| 39 | + |
| 40 | +``` |
| 41 | +/https?:\\/\\/(www\\.)?\[-a-zA-Z0-9@:%.\_\\+~#=\]{2,256}\\.\[a-z\]{2,6}\\b(\[-a-zA-Z0-9@:%\_\\+.~#()?&//=\]\*)/ |
| 42 | +``` |
| 43 | + |
| 44 | +* [Protocol Optional](https://www.regexpal.com/?fam=104035) |
| 45 | + |
| 46 | +``` |
| 47 | +/(https?:\\/\\/)?(www\\.)?\[-a-zA-Z0-9@:%.\_\\+~#=\]{2,256}\\.\[a-z\]{2,6}\\b(\[-a-zA-Z0-9@:%\_\\+.~#?&//=\]\*)/ |
| 48 | +``` |
| 49 | + |
| 50 | +#### 7\. IP Address |
| 51 | + |
| 52 | +* [IPv4 address](https://www.regexpal.com/?fam=104036) |
| 53 | +``` |
| 54 | +/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/ |
| 55 | +``` |
| 56 | +* [IPv6 address](https://www.regexpal.com/?fam=104037) |
| 57 | +``` |
| 58 | +/(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/ |
| 59 | +``` |
| 60 | + |
| 61 | +* Both [IPv4, IPv6 addresses](https://www.regexpal.com/?fam=104038) |
| 62 | +``` |
| 63 | +/((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/` |
| 64 | +``` |
| 65 | + |
| 66 | +#### 8\. Dates |
| 67 | + |
| 68 | +* Date Format [YYYY-MM-dd](https://www.regexpal.com/?fam=104039) using separator `-` |
| 69 | +`/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/` |
| 70 | +* Date Format [dd-MM-YYYY](https://regexr.com/?346hf) using separators `-` or `.` or `/` |
| 71 | +`/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/` |
| 72 | +* Date Format [dd-mmm-YYYY](https://regexr.com/39tr1) using separators `-` or `.` or `/` |
| 73 | +`/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/` |
| 74 | + |
| 75 | +#### 9\. Time |
| 76 | + |
| 77 | +* Time Format [HH:MM 12-hour](https://www.regexpal.com/?fam=104040), optional leading 0 |
| 78 | + |
| 79 | +`/^(0?[1-9]|1[0-2]):[0-5][0-9]$/` |
| 80 | + |
| 81 | +* Time Format HH:MM 12-hour, optional leading 0, **[Meridiems (AM/PM)](https://www.regexpal.com/?fam=104041)** |
| 82 | + |
| 83 | +`/((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))/` |
| 84 | + |
| 85 | +* Time Format [HH:MM 24-hour](https://www.regexpal.com/?fam=104042) with leading 0 |
| 86 | + |
| 87 | +`/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/` |
| 88 | + |
| 89 | +* Time Format [HH:MM 24-hour, optional leading 0](https://www.regexpal.com/?fam=104043) |
| 90 | + |
| 91 | +`/^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/` |
| 92 | + |
| 93 | +* Time Format [HH:MM:SS 24-hour](https://www.regexpal.com/?fam=104044) |
| 94 | + |
| 95 | +`/(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)/` |
| 96 | + |
| 97 | +#### 10\. HTML Tags |
| 98 | + |
| 99 | +* [Elements with Attributes](https://www.regexpal.com/95941) `/<\/?[\w\s]*>|<.+[\W]>/` |
| 100 | + |
| 101 | +#### 11\. Javascript Handlers |
| 102 | + |
| 103 | +* [Inline JS handler](https://www.regexpal.com/?fam=104055) `/\bon\w+=\S+(?=.*>)/` |
| 104 | +* [Inline JS handler with element](https://www.regexpal.com/94641) `/(?:<[^>]+\s)(on\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/` |
| 105 | + |
| 106 | +#### 12\. Slug |
| 107 | + |
| 108 | +* [Slug](https://www.regexpal.com/?fam=104056) `/^[a-z0-9]+(?:-[a-z0-9]+)*$/` |
| 109 | + |
| 110 | +#### 13\. Match Duplicates in a String |
| 111 | + |
| 112 | +* [Search Duplicates](https://www.regexpal.com/?fam=104060) `/(\b\w+\b)(?=.*\b\1\b)/` |
| 113 | + |
| 114 | +#### 14\. Phone Numbers |
| 115 | + |
| 116 | +* [International Phone Numbers](https://www.regexpal.com/?fam=99127) – with optional country code/extension |
| 117 | +``` |
| 118 | +`/* International Phone Numbers */` |
| 119 | +/^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:``#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/ |
| 120 | +``` |
| 121 | + |
| 122 | +_Note:_ Use regex for validating phone numbers **only** if you don’t have the choice to use a library. There are [several libraries](https://stackoverflow.com/a/15644461/4717533) that handle phone numbers more accurately and should be used instead. |
| 123 | + |
| 124 | +#### 15\. File Path |
| 125 | + |
| 126 | +* [File Path with Filename and extension](https://www.regexpal.com/?fam=104047) |
| 127 | + |
| 128 | +``` |
| 129 | +/((\\/|\\\\|\\/\\/|https?:\\\\\\\\|https?:\\/\\/)\[a-z0-9 \_@\\-^!#$%&+={}.\\/\\\\\\\[\\\]\]+)+\\.\[a-z\]+$/ |
| 130 | +``` |
| 131 | + |
| 132 | +* File Path with optional Filename, extension |
| 133 | + |
| 134 | +``` |
| 135 | +/^(.+)/(\[^/\]+)$/ |
| 136 | +``` |
| 137 | + |
| 138 | +* [File Name with extension](https://www.regexpal.com/?fam=104048) having 3 chars |
| 139 | + |
| 140 | +``` |
| 141 | +/^\[\\w,\\s-\]+\\.\[A-Za-z\]{3}$/ |
| 142 | +``` |
| 143 | + |
| 144 | +### Additional Regexes |
| 145 | + |
| 146 | +#### 1\. Zip codes |
| 147 | + |
| 148 | +There is **NO** single Regex that can handle all zip codes given that zip codes around the world do not follow a common pattern. Here is a [list](https://stackoverflow.com/a/7185241/4717533) that contains Regex specific to each country. |
| 149 | + |
| 150 | +#### 2\. Payment Validation |
| 151 | + |
| 152 | +Here is a [link](https://www.regular-expressions.info/creditcard.html) that contains regex for validating leading Credit cards like Visa, Mastercard and so on. |
| 153 | + |
| 154 | +#### 3\. Identity Documents |
| 155 | + |
| 156 | +* Social Security Number – [Ref](https://www.codeproject.com/Articles/651609/Validating-Social-Security-Numbers-through-Regular) |
| 157 | + |
| 158 | +``` |
| 159 | +/\* can use either hypen(-) or space( ) character as separator \*/ |
| 160 | +
|
| 161 | +/^((?!219-09-9999|078-05-1120)(?!666|000|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4})|((?!219 09 9999|078 05 1120)(?!666|000|9\\d{2})\\d{3} (?!00)\\d{2} (?!0{4})\\d{4})|((?!219099999|078051120)(?!666|000|9\\d{2})\\d{3}(?!00)\\d{2}(?!0{4})\\d{4})$/ |
| 162 | +``` |
| 163 | + |
| 164 | +* Passport – `/^[A-PR-WY][1-9]\d\s?\d{4}[1-9]$/` |
| 165 | + |
| 166 | +#### References |
| 167 | +* [https://www.codeproject.com/validate-passport-number](https://www.codeproject.com/Questions/1046445/How-to-validate-passport-number-using-javascript) |
| 168 | +* [https://gist.github.com/](https://gist.github.com/nerdsrescueme/1237767) |
| 169 | +* [https://code.tutsplus.com/regular-expressions](https://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149) |
| 170 | +* [https://www.regular-expressions.info](https://www.regular-expressions.info/) |
| 171 | +* [http://www.regexlib.com/](http://www.regexlib.com/) |
| 172 | +* [https://projects.lukehaas.me/regexhub/](https://projects.lukehaas.me/regexhub/) |
| 173 | +* [http://wiki.zoolz.com/commonly-used-regular-expressions/](http://wiki.zoolz.com/commonly-used-regular-expressions/) |
| 174 | +* [https://www.smashingmagazine.com/advanced-regular-expressions/](https://www.smashingmagazine.com/2009/05/introduction-to-advanced-regular-expressions/) |
0 commit comments