Learn Regular Expressions (Beginners Guide)
Regular expressions, also shortened as regex, are used to find specific patterns, to replace characters in a string using specific patterns, or mostly used to validate inputs to match specific patterns.

What is a Regular Expression?
A regex (regular expression) is a sequence or pattern of characters used to find part of a string. We will go through a few simple regex examples that can be used for finding specific patterns, replacing them, or just validating a string. Regular expressions are used in many applications like Notepad++ and Microsoft Word. It is also used in web development process of web applications. This post explains the basic usage of regular expressions and how to create regular expressions in web applications.
How to Write a Regular Expression?
Regular expressions are created using a special syntax; PHP regex and JavaScript regex both have / at the start and end that indicates it is a regular expression. To use a regex properly, you need to have at least a basic understanding of syntax. Let's take a look at a basic special characters and quantifiers mostly used to write a regex (regular expression).
- ^ regex quantifier indicates the start of a string or line in a pattern. For example, "^hello" would mean the string or line should start with "hello".
- $ regex quantifier indicates the end of a string or line in a pattern. For example, "bye$" would mean the string or line must end with "bye".
- * regex quantifier indicates zero or more occurrences of the preceding character or group of characters. For example, "123*A" would mean the string has a "123" number and "A" could appear any number of times or not at all.
- + regex quantifier indicates the preceding character or group of characters occurs at least one time and could occur more than one time. For example, "abc+" would mean the string should have "ab" and "c" at least one time, or more. So "abc" and "abccc" both will pass the match, but only "ab" will not.
- ? regex quantifier indicates zero or one occurrences of the preceding character or group of characters. For example, "https?" would mean the "s" at that position can occur only once or can be missing. So "htpp" and "https" both will pass the match, but "httpss" will not.
- () regex captures a group of characters. For example, "(www\.)" will mean the string should be "www." as a whole group. Regex groups are also used for references in the following patterns.
- [] regex indicates a set of characters or any of the characters inside brackets. For example, "123[a-zA-z]" will mean the string should be "123" and any alphabet should be either lowercase or uppercase. So "123H" will be a correct match.
- {} regex limits the occurrence of the preceding character or group of characters to a specific number or between a specific range. For example, "a{2,4}" will mean the letter "a" should appear between two and four times. In this case, "aa", "aaa", and "aaaa" will be valid matches.
Regular Expression Examples:
The following table shows the different quantifiers and logical syntax of regular expressions. This table can be used as a quick reference for writing regular expressions and for a basic understanding of regular expressions.
| Expression | Definition | Example | Sample |
|---|---|---|---|
| ^ | Start of string/line | /^abc/ | abcword |
| $ | End of string/line | /abc$/ | wordabc |
| \d | Any digit | /word\d/ | word5 |
| \w | Any alphanumeric or underscore | /123\w/ | 123Z |
| [0-9] | Numbers from 0 to 9 | /word[0-9]/ | word1 |
| [a-z] | Letters from a to z | /123[a-z]/ | 123a |
| [A-Z] | Letters from A to Z | /123[A-Z]/ | 123W |
| + | Minimum one occurrence | /\d+/ | 123456 |
| * | Zero or more occurrences | /\d+A*/ | 123AAA or 123 |
| ? | Zero or one occurrence | /\d+ab?c/ | 123abc or 123ac |
| . | Any character that is not a line break | /x.z/ | xyz |
| {3} | Exactly three occurrences | /a{3}/ | aaa |
| {2,4} | Minimum two and maximum four occurrences | /a{2,4}/ | aa |
| {2,} | Two or more occurrences | /\w{2,}/ | word_word |
Commonly Used Regular Expressions:
Web developers most commonly use certain regex patterns to validate or find and replace certain parts of strings. The following are mostly used regular expressions and can be used in PHP as well as JavaScript. With the following list of commonly used regular expressions, you can verify only numbers, verify only alphabets, verify and validate an email address, and validate a URL.
Validate Only Digits with Regular Expression:
The following table shows a number regex pattern to verify and validate only numbers:
| Expression | Sample | Result |
|---|---|---|
| /^[0-9]+$/ | 123456 | Pass |
| 123abc | Fail |
Validate Only Alphabets with Regular Expression:
The following table shows an alphabet regex pattern that can be used to verify and validate only alphabets using this regular expression:
| Expression | Sample | Result |
|---|---|---|
| /^[a-zA-Z]+$/ | abcXYZ | Pass |
| abcXYZ12 | Fail |
Validate Email Address with Regular Expression:
The following table shows the most commonly used email validation regex that is used to verify and validate email format:
| Expression | Sample | Result |
|---|---|---|
| /^[\w\-\.]+\@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/ | [email protected] | Pass |
| user@domain | Fail | |
| userdomain.com | Fail |
Validate URL with Regular Expression:
The following table shows another commonly used URL regular expression pattern to verify and validate a URL:
| Expression | Sample | Result |
|---|---|---|
| /^((https?|ftp):\/\/)(www\.)?[\w\-]+\.[a-z]{2,4}\/?$/ | https://domain.com | Pass |
| http://domain.com | Pass | |
| http://www.domain.com | Pass | |
| www.domain.com | Fail |