Regular Expressions in JavaScript
What is RegEx? How to create a RegEx? What is a test method in RegEx?
Introduction
What is a Regular Expression or RegEx? So, Regular Expressions are a way to describe patterns in string data. In other words, Regular Expressions are patterns used to match character combinations in strings.
Creating a RegEx Expression
RegEx is a type of object, which can be constructed with RegEx Constructor or as a literal value by enclosing a pattern in forward slash (/) characters.
example:-
let reg1 = new RegEx("ABC");
in the above example, a regular expression is created using a RegEx constructor with a pattern ABC. now, let's see another example to create a regex.
let reg2 = /abc/;
in the above example, a regular expression is created using literal value by enclosing the abc pattern in a forward slash.
Test Method
If passed a string it will return a boolean telling whether the string contains a match of the pattern in the expression.
example:-
console.log(/abc/.test("abcde"));
here the test function will check whether the complete pattern abc is contained in the expression abcde and returns a boolean based on that. So here the output will be true as the test method will find the abc pattern at the start of the expression itself.
console.log(/abc/.test("abxdce"));
here the output will be false as the test method will not be able to match the pattern abc in the given expression abxdce.
Understanding + and ? in a RegEx
When we put + after some pattern in regex it matches one or more characters.
? (Question mark) makes a part of a pattern optional, it may match or may not.
example:-
let reg1 = /colou?r/;
so here the u is made optional by applying? after, so to match the pattern for the above expression it may contain u or may not.
console.log(reg1.test("colour"));
output- true
console.log(reg1.test("color"));
output- true
console.log(reg1.test("coulor"));
output- false
To use + or any operator on more than one element at a time use parenthesis.
example:-
let reg1 = /boo+(hoo+)+/;
Set of Characters
Several common character groups have their built-in shortcuts. Digits are one of them: \d
means the same thing as [0-9]
.
| Any digit character |
| An alphanumeric character (“word character”) |
| Any whitespace character (space, tab, newline, and similar) |
| A character that is not a digit |
| A non-alphanumeric character |
| A non-whitespace character |
| Any character except for Newline |
To indicate a pattern should occur n number of times, use braces putting {n} after an element, which requires it to occur exactly n times.
example:- {2,4} Here the range specifies that element must occur at least twice and at most 4 times.
Let's understand the above logic with an example. So we want to match a date in DD-MM-YYYY format, what should be the regex for that?
let date = /\d{1,2}-\d{1,2}-\d{4}/;
console.log(date.test("1-30-2023"));
the above code will print true on the console as it is matching the date regex. Reason for the output: the DD and MM could have at least one digit and at most 2 digits so for DD it is one and for MM it is 2 in the "1-30-2023" example and the YYYY should have exactly 4 digits which is also matching.