Some Regular Expression (RegEx) Syntax Worth To Used

Handhika Yanuar Pratama
2 min readJan 18, 2021

Regular Expression is a sequence of characters that define a search pattern. Usually such patterns are used by string-searching algorithms for “find” or “find and replace” operations on strings, or for input validation. It is a technique developed in theoretical computer science and formal language theory. — Wikipedia

I just learn regex in the end of 2020, and today i learn a form validation using RegEx. Here i found a few syntax that really save my time. Hope for you too.

Name Checker

As we know, that most of form has a first and last name on it. Here is the form name validation. The most, important thing below is the number (2,8) it means we will take the validation between 2 until 8, if not, the form will be wrong.

/^([a-zA-Z0-9_\.\-]+)@([a-zA-Z0-9_\.\-]+)\.[a-zA-Z]{2,8}$/

Email Validation

Email is one of important things in this modern time, we use our email to get subscription of a things or many others. But as a developer, i believe sometimes we forget to make validation, so fake user can put wrong value. So with this regex, it can save your time.

/^([a-zA-Z0-9_\.\-]+)@([a-zA-Z0-9_\.\-]+)\.[a-zA-Z]{2,5}$/

Phone Number Validation

Phone number is vital, everyday we always bring smartphone right? So, this simple regex perhaps can help to build a US phone number format. #cmiiw

/^\(?\d{3}\)?[-.]?\d{3}[-.]?\d{4}$/

Zip Number Validation

Zip number is somehow always being in validation email, it used to validate zip code. This is the way i build it, you can follow or edit as you likes

/^[0-9]{5}(-[0-9])?$/

Here is the example output, when using it

Hope it usefull!

Source code for the code above

--

--