Photo by Christina @ wocintechchat.com on Unsplash
A Beginner's Guide to Regular Expressions
Simplifying the complex topic of regex in JavaScript
Regular expressions, a.k.a. regex or regexp, are a sequence of characters that specify a matched pattern in any text. They are extremely powerful for searching and manipulating text. What follows here is my attempt at simplifying the use of regular expressions in JavaScript. Enjoy!
So what is a Regular Expression?
As I stated, a regular expression is a sequence of characters that defines a search pattern. It consists of normal characters - like letters and numbers, but can also contain special characters - like ^ $ . * + ? [ ] \ | ( ) { }
that carry special meaning.
For example, the regular expression /ant/
means: the letter a, followed by the letter n, followed by the letter t. This regex will match any string that contains the sequence 'ant'.
How to Create a Regular Expression in JavaScript
There are two ways to create a regular expression in JavaScript:
Using a regular expression literal which consists of the pattern enclosed between slashes:
const myRegex = /ant/;
Or calling the RegExp constructor function:
const myRegex = new RegExp('ant');
Regular Expression Methods in JavaScript
Once we have a regular expression, we can use various methods to test, match, search or replace text. The test() method returns a boolean value indicating whether the pattern exists in the string. It returns true if found, and false if not found. The match() method returns an array containing all matches of the pattern in the string. The matches are returned in the order they are found. The search() method returns the index of the first match of the pattern in the string. It returns -1 if no match is found. The replace() method returns a new string where the matched pattern is replaced with the replacement text. By default, it only replaces the first match.
.test() // returns a boolean if a pattern exists in a string
.match() // returns an array of all pattern matches
.search() // returns the index of the first match
.replace() // returns a new string with some or all matches replaced
Code sample:
const str = 'developer_ant';
const myRegex = /ant/;
myRegex.test(str); // Returns true
str.match(myRegex); // Returns [ 'ant' ]
str.search(myRegex); // Returns 10
str.replace(myRegex, 'Anthony'); // Returns "developer_Anthony"
Regular Expression Modifiers
We can also add modifiers to our regular expression to make it case-insensitive or global. The ' i ' modifier makes the regex case insensitive, allowing it to match upper and lowercase letters. The ' g ' modifier makes the regex global, enabling it to find all matches rather than stopping at the first match.
i - Case-insensitive search
g - Global search, finds all matches rather than stopping at the first match
Code sample:
const str1 = 'developer_ant';
const str2 = 'DEVELOPER ANTHONY is the developer_ant';
const myRegex = /ant/gi; // Finds all instances of ant or ANT in a string
myRegex.test(str1); // Returns true
str1.match(myRegex); // Returns [ 'ant' ]
myRegex.test(str2); // Returns true
str2.match(myRegex); // Returns [ 'ANT', 'ant' ]
Regular Expression Syntax
Some common regex syntax includes anchors, character classes, quantifiers, and groups. The ' ^ ' anchor matches the beginning of the input string. The ' $ ' anchor, also referred to as bling, matches the end of the input string. Character classes such as ' \d ' match digits and ' \s ' match whitespace. Quantifiers like * and + match 0 or more and 1 or more occurrences of something. Groups indicated with ' ( ) ' allow you to target and capture parts of a pattern.
^ - Matches beginning of input
$ - Matches end of input
. - Matches any single character
? - Makes something optional
() - Capture group
[] - Character set
{} - Quantifier
Code sample:
const regex1 = /^Hello/; // matches "Hello World" but not "world Hello"
const regex2 = /\d+$/; // matches one or more digits at the end ("E40", "5051")
const regex3 = /colou?r/; // matches both color and colour
const regex4 = /(abc)/; // captures "abc" as a group
Utilizing Regular Expressions with .split()
The .split() method in JavaScript allows you to split a string into an array by a separator. Most of the time this separator is a simple string.
'developer_ant'.split('_') // returns ['developer', 'ant']
But did you know, that this separator can also be a regular expression pattern?
Code sample:
const str = "developer_ant";
str.split(/_/); // Split on underscore, returns ["developer", "ant"]
str.split(/e/); // Split on the letter 'e', returns ["d", "v", "lop", "r_ant"]
The ability to split by a regex pattern is super useful! Having this added tool for breaking up strings into unique parts only adds to the complexity of Regular Expressions.
Regular expressions can seem intimidating at first, however, like most Javascript, they become easier with time and practice. They are super useful for finding and manipulating patterns in text and are well worth the headache of learning.
The Complexity of the Universe and Regular Expressions
As complex as regular expressions may seem, they pale in comparison to the complexity of the universe. Some ancient astronaut theorists suggest that our universe could actually be a vast and intricate simulation, computed by something infinitely beyond our comprehension... kind of like a cosmic regex!!
So, while regular expressions may seem mystifying at first, just remember, that they likely have far fewer edge cases and exceptions than whatever grand code makes up our universe. Mastering regex is a walk in the park compared to understanding existence itself.
With practice, regular expressions start to make sense and become an extremely valuable tool. They may even give you a glimpse into the mind-boggling intricacy of our simulated reality! =)