Search This Blog

Saturday, October 19, 2013

USEFUL REGULAR EXPRESSIONS

In Web Development, there have forms we need to secure and validate using Regular Expression, in some cases are registration form, transaction forms such as valid email, valid postal code, matching strong password and securities.
In this article, i have compiled the useful of JavaScript Regular Expression, this is the most common regular expression we use in web development such validating forms. These Regular Expression will not match in common cases, it even fewer special cases you might be need, because any Regular Expression can be written broadly and narrowly, meaning, broadly is a match a lot of pattern and narrowly is very restricted in matching.

1. Matching names

Regex:
1/^([A-Z][A-Za-z.'\- ]+) (?:([A-Z][A-Za-z.'\-]+) )?([A-Z][A-Za-z.'\-]+)$/
String that matches:
1John Doe
2John de Doe
3John Doe Doe
4John Doe D. Doe J.r III
Pattern can match any valid names with first character capitalize.

2. Matching postal codes

Regex:
1/^\d{4,5}(?:-\d{4,5})?$/
String that matches:
175087
210010-6543
Pattern can match postal codes broadly, they match mostly US postal code.

3. Matching email addresses

Regex:
1/^[\w%_\-.\d]+@[\w.\-]+.[A-Za-z]{2,6}$/
String that matches:
1someone@istockphp.com
2someone@istockphp.co.uk
3someone@istockphp.net
4some-one@istockphp.edu
Pattern can match any valid email format, however this is most common use for validating email in registration form.

4. Matching urls

Regex:
1/^(http|https):\/\/[\w.\-]+(\.[\w\-]+)+[/#?\w\-.,@?^=&%:;/~\\+#]+$/
String that matches:
1http://www.istockphp.com
2 
3 
4http://istockphp.com
5 
6 
7http://blog.istockphp.com
8 
9 
10https://www.istockphp.com
11 
12 
13http://www.istockphp.com/product_page.html
14 
15 
16http://www.istockphp.com/images/image.gif
17 
18 
19http://www.istockphp.com/product/
20 
21 
22http://www.istockphp.com/product/3456
23 
24 
25http://www.istockphp.com/product_page.php?product=28
26 
27 
28http://www.istockphp.com?product=28&color=blue
29 
30 
31http://www.istockphp.com#details
32 
33 
34http://255.255.255.255
In the form, might have a extra field to input the user for URL, this regex validate the valid url.

5. Matching decimal numbers

Regex:
1/^(\d*\.\d+|\d+)$/
String that matches:
15.4
2314.35634
30.123
4.345
525
Pattern match a valid decimal numbers.

5. Matching currencies

Regex:
1/^(\$|£|¥)(\d*\.\d+|\d+)$/
String that matches:
1$50
2$43.23
3$0.39
4$.60
5£498.10
6¥12
Pattern can match the right currencies, in reg ex you can add more currencies to match.

6. Matching ip addresses

Regex:
1/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
String that matches:
1255.255.255.255
20.0.0.0
367.52.159.38
4067.052.159.038
Pattern can match the right ip address, obviously the ip address ’999.999.999.999′ will not match.

7. Matching dates

Regex:
1/^\d{1,4}[-/](0?[1-9]|1[0-2])[-/](0?[1-9]|[12][0-9]|3[01])$/
String that matches:
12000-11-15
22000-6-9
32000-06-09
42000/6/9
Pattern can matches the date format, year, month and day.

8. Matching time in 12hrs

Regex:
1/^(0?[1-9]|1[0-2]):[0-5][0-9]([aApP][mM])?/
String that matches:
102:34
22:34pm
32:34PM
402:34
512:34
612:59
71:00PM
Pattern can match 12 hrs time. it match up to 12:59 am and pm.

9. Matching time in 24hrs with Time zone

Regex:
1/^([0-1]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?( ([A-Z]{3}|GMT [-+]([0-9]|1[0-2])))?$/
String that matches:
10:02
22:34
302:34
412:34
512:34:56
612:34 EST
712:34 GMT -5
812:34 GMT +12
923:45
Pattern can match 24 hrs time. it match up to 23:59 including timezone.

10. Matching HTML tags

Regex:
1/^<(?:([A-Za-z][A-Za-z0-9]*)\b[^>]*>(?:.*?)|[A-Za-z][A-Za-z0-9]*\b[^/>]*/>)$/
String that matches:
1<strong>Bold</strong>
2<em>Emphazied</em>
3<b>Bold</b>
4<i>Italics</i>
5<span id="foo">this is the text</span></pre>
6 
7<hr />
8 
9<pre>
10<strong>Bold</strong>
11<em>Emphazied</em>
12<b>Bold</b>
13<i>Italics</i>
14<span id="foo">this is the text</span></pre>
15 
16<hr />
17 
18<pre>
Pattern can match HTML tags.

11. Validating strong passwords

Regex:
1/^(?=.*\d)(?=.*[~!@#$%^&*()_\-+=|\\{}[\]:;<>?/])(?=.*[A-Za-z])(?=.*[a-z])\S{8,15}$/
String that matches:
1mypassword69[]
2mypass';i#%^9[]
This pattern help to make sure user give a secure password that’s harder for hacker to crack it.
Pattern for password requires.
  • Password Any character, except space.
  • Password at least 8 characters long
  • And not more than 15 characters.
  • Password must have a number.
  • Password must have a special character.

12. Matching credit card numbers

Regex:
1/^(?:3[47]\d{2}([\- ]?)\d{6}\1\d{5}|(?:4\d{3}|5[1-5]\d{2}|6011)([\- ]?)\d{4}\2\d{4}\2\d{4})$/
String that matches:
1American Express
2370012345612345
33700 123456 12345
43700-123456-12345
5 
6Visa
74000123412341234
84000 1234 1234 1234
94000-1234-1234-1234
10 
11Mastercard
125100123412341234
135100 1234 1234 1234
145100-1234-1234-1234
15 
16Discover
176011123412341234
186011 1234 1234 1234
196011-1234-1234-1234
Pattern can match 4 major of credit card numbers, American Express, Visa, MasterCard, Discover.

No comments:

Post a Comment