JavaScript Regex
JavaScript Regex
/^a...s$/
The above code defines a RegEx pattern. The pattern is: any five letter string
starting with a and ending with s.
A pattern defined using RegEx can be used to match against a string.
Expression String Matched?
abs No match
alias Match
Alias No match
An abacus No match
Create a RegEx
There are two ways you can create a regular expression in JavaScript.
For example,
const regex = new RegExp(/^a...s$/);
[Link]([Link]('alias')); // true
Run Code
In the above example, the string alias matches with the RegEx pattern /^a...s$/.
Here, the test() method is used to check if the string matches the pattern.
There are several other methods available to use with JavaScript RegEx. Before
we explore them, let's learn about regular expressions themselves.
If you already know the basics of RegEx, jump to JavaScript RegEx Methods.
MetaCharacters
Metacharacters are characters that are interpreted in a special way by a RegEx
engine. Here's a list of metacharacters:
[] . ^ $ * + ? {} () \ |
[] - Square brackets
Square brackets specify a set of characters you wish to match.
a 1 match
ac 2 matches
[abc]
Hey Jude No match
abc de ca 5 matches
Here, [abc] will match if the string you are trying to match contains any of
the a, b or c.
You can also specify a range of characters using - inside square brackets.
[a-e] is the same as [abcde].
[1-4] is the same as [1234].
[0-39] is the same as [01239].
You can complement (invert) the character set by using caret ^ symbol at the
start of a square-bracket.
[^abc] means any character except a or b or c.
[^0-9] means any non-digit character.
. - Period
A period matches any single character (except newline '\n').
Expression String Matched?
a No match
ac 1 match
..
acd 1 match
a 1 match
^a abc 1 match
bac No match
abc 1 match
^ab
acb No match (starts with a but not followed by b)
$ - Dollar
The dollar symbol $ is used to check if a string ends with a certain character.
Expression String Matched?
a 1 match
a$ formula 1 match
cab No match
* - Star
The star symbol * matches zero or more occurrences of the pattern left to it.
Expression String Matched?
mn 1 match
man 1 match
woman 1 match
+ - Plus
The plus symbol + matches one or more occurrences of the pattern left to it.
Expression String Matched?
man 1 match
woman 1 match
? - Question Mark
The question mark symbol ? matches zero or one occurrence of the pattern left
to it.
Expression String Matched?
ma?n mn 1 match
man 1 match
maan No match (more than one a character)
woman 1 match
{} - Braces
Consider this code: {n,m}. This means at least n, and at most m repetitions of the
pattern left to it.
Expression String Matched?
Let's try one more example. This RegEx [0-9]{2, 4} matches at least 2 digits but
not more than 4 digits.
Expression String Matched?
1 and 2 No match
| - Alternation
Vertical bar | is used for alternation (or operator).
Expression String Matched?
cde No match
() - Group
Parentheses () is used to group sub-patterns. For example, (a|b|c)xz match any
string that matches either a or b or c followed by xz
Expression String Matched?
ab xz No match
\ - Backslash
Backslash \ is used to escape various characters including all metacharacters.
For example,
\$a match if a string contains $ followed by a. Here, $ is not interpreted by a
RegEx engine in a special way.
If you are unsure if a character has special meaning or not, you can put \ in front
of it. This makes sure the character is not treated in a special way.
Special Sequences
Special sequences make commonly used patterns easier to write. Here's a list of
special sequences:
football Match
\bfoo
a football Match
a football No match
\B - Opposite of \b. Matches if the specified characters are not at the beginning or
end of a word.
Expression String Matched?
a football Match
t\n\r\f\v].
a b 2 matches (at a b)
\S
No match
Tip: To build and test regular expressions, you can use RegEx tester tools such
as regex101. This tool not only helps you in creating regular expressions, but it
also helps you learn it.
Now you understand the basics of RegEx, let's discuss how to use RegEx in your
JavaScript code.
exec()
Executes a search for a match in a string and returns an array of
information. It returns null on a mismatch.
matchAll(
Returns an iterator containing all of the matches.
)
Tests for a match in a string and returns the index of the match. It
search()
returns -1 if the search fails.
Searches for a match in a string and replaces the matched substring with
replace()
a replacement substring.
Flags Description
// performing a replacement
const result1 = [Link](/hello/, 'world');
[Link](result1); // Hello world hello
function validatePhone(num) {
// take input
let number = prompt('Enter a number XXX-XXX-XXXX');
validatePhone(number);
Run Code
Output
function validateEmail(email) {
// take input
let email = prompt('Enter an email: ');
validateEmail(email);
Run Code
Output