7 Most Useful Regular Expression Snippets

Regular Expressions can make the life of a programmer very easier. With Regular Expressions, validations just melt down to a single line of code in cases where you are ready to write a clumsy code extending around 50 lines. Here I am presenting a list of 7 most useful Regular Expression snippets to help you.

Note : These snippets show the basic usage of regular expressions. Whole range of validations has not been covered. Only the shown formats have been validated. Please add your comments to make them better!

Demo:

Validate file :

Validate email :

Validate date :

Validate IP :

Validate Number :

Validate Time : (HH:MM or HH:MM:SS or HH:MM:SS.mmm)

Validate Zip : (12345 or 12345-6789)

1. Validating File Name and Extension

function validate_filename(str)
{
str = str.replace(/^s|s$/g, ""); //trims string
return /^([w-_]+).(asp|html|htm|shtml|php|txt)$/.test(str)
}

2. Validating an Email Address

function validate_email(str) {
return /^([w-_.]+)(.[w-_.]+)*@([w-]+)(.[w]{2,7})(.[a-z]{2})?$/i.test(str);
}

3. Validating a Number

function  validateNumeric( str ) {
return objRegExp = /(^-?dd*.d*$)|(^-?dd*$)|(^-?.dd*$)/.test(str);
}

4. Validating Date (mm/dd/yyyy)

function validate_date(str) {
return /(?:0[1-9]|[12][0-9]|3[01])/(?:0[1-9]|1[0-2])/(?:19|20d{2})/.test(str);
}

5. Validating Time (HH:MM or HH:MM:SS or HH:MM:SS.mmm)

function validate_time(str) {
return /^([1-9]|1[0-2]):[0-5]d(:[0-5]d(.d{1,3})?)?$/.test(str);
}

6. Validating IP Address

function validate_ip(str) {
return /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/.test(str);
}

7. Validating Zip Code(12345-6789)

function validate_zip(str) {
return /(^d{5}$)|(^d{5}-d{4}$)/.test(str);
}

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.

Website: http://www.webspeaks.in/

7 thoughts on “7 Most Useful Regular Expression Snippets

  1. I like it — and follow it, since I use regular expressions. However, I would suggest 2 or 3 sentences of explanation beneath each expression to make it read faster — especially for those who haven’t used regular expressions. (I remember how intimidated I was when I first came in to contact with this ‘gobbledy gook’ 🙂

  2. All of these are broken, and does validate data, that it shouldn’t or it doesn’t validate data, that it should.
    – 256.265.345.315 is not a valid IP address, although it would be validated as such.
    – 14/38/0009 is not a valid date, although it would be validated as such.
    – b-ware.html would be validated as a non valid filename, although it is.
    – the email address validator is plain broken, doesnt handle `-` for example.
    – i cannot say anything about the zip code, as i dont know what country this is a zip code, my country has 4 digit zip codes, others, like the UK has letters and digits too.

  3. Some mistaken checks:

    “+” is allowed in an email address…

    “abc.foo.php” IS a a valid filename…

    “5/5/2010” IS a valid date.

    “22:25:59” IS in fact a valid time in 24H format.

  4. The date regexp is very bad, you do not validate 30/31 months or that February may have 28 or 29 days.
    the one i use does that, but still “5/5/2010” may not be a valid date if the programmer wants a specific date format, because if you say “5/5/2010” is a valid date, “10-10-2010″,”2010-10-10” (…) all depends on the format you want to validate, but still is a bag regexp.
    Try “/^(|(([012][0-9]|[3][01])-([0][13578]|[1][02])-([0-9]{4}))|(([012][0-9]|[3][0])-([0][469]|[1][1])-([0-9]{4}))|(([01][0-9]|[2][0-8])-(02)-([0-9]){4})|((29)-(02)-([0-9]{2}[02468][048]))|((29)-(02)-([0-9]{2}[13579][26])))$/” its bigger but better

Comments are closed.