What is RegEx?
A Regular Expression (often shortened to regex or regexp), is a sequence of characters that specifies a search pattern in a string.
This expression is often used by string-searching algorithms for find or find and replace operations on strings. It can also be used for input validation (server-side checking that the input supplied by a user is of the expected form).
Regular expressions are supported by most general-purpose programming languages- either natively or with libraries. I will be talking about regular expressions mainly in the context of JavaScript, but they are also commonly utilized by Python, C++, and Java.
Writing a Regular Expression
Regular expressions can be constructed in two different ways.
The first is as a regular expression literal:
const regex = /hello/;
In the code above, our search value is stored between forward slashes. This is assigned to the variable regex.
The second way to construct regular expressions is by calling the constructor function of the RegExp object:
const re = new RegExp( 'hello' );
In the code above, we are passing our search value into our RegExp object as a string. We are assigning this to the variable re.
The result of either method of constructing a regular expression will be a regex object. Both returns from the examples above will have the same methods and properties attached to them.
Regular Expression Methods
In JavaScript, there are two main methods that we can use to test regular expressions.
The examples below will be using the regular expression literal syntax, though constructor functions would also work.
RegExp.prototype.test()
When given a search value (our regular expression), this method will test a given string to see whether a match is found. The return value will be a boolean, returning true if a match is found and false otherwise.
For example:
let regex = /hello/;
let str = 'hello world';
let result = regex.test(str);
console.log(result);
// => true
RegExp.prototype.exec()
Like the RegEx test method, this method takes a regular expression and a string to test. The return, however, will be an array containing all matched groups.
For example:
let regex = /hello/;
let str = 'hello world';
let result = regex.exec(str);
console.log(result);
//=> [ 'hello', index: 0, input: 'hello world', groups: undefined ]
In the example above:
- 'hello' represents our matched pattern
- index represents where the regular expression starts
- input represents the string passed into our exec method
Special Characters
This is an extremely basic starting point for using these methods, as there is a seemingly infinite amount of knowledge to seek out relating to RegEx.
Thankfully, you do not have to fully immerse yourself in that world to understand and make use of these methods. The following is a broad look into frequently used additions to our regular expressions for more precise searches.
Flags
Regular expressions accept up to five modifiers, or flags, that can make your search value(s) more specific. The two most common flags are:
- g - Global search (returns all matches, not just the first instance of a match)
- i - Case-insensitive search (returns matches regardless of casing)
These flags are placed after our two slashes, and can be combined like so:
let regex = /John/gi;
console.log(regex.test('jOhn john JOHN"));
//=>true
This will match all three occurrences of 'John', even though they have different casing.
Meta-characters
In addition to flags, our regular expressions will accept meta-characters, which communicate a specific query for our search. Because there are many meta-characters, I will only run through a few common ones.
- \d - Matches any digit characters
- \D - Matches anything that is NOT a digit
- \w - Matches any word character
- \W - Matches anything that is NOT a word character
- \s - Matches any form of white space
- \S - Matches any NON white space character
For example:
let regex = /\s/;
console.log(regex.test('hello world!'));
//=>true
console.log(regex.test('hey'));
//=>false
In the above example, our test method finds a match in the space in "hello world!", but fails to find an empty space in "hey".
Conclusion
As mentioned, this article is meant to serve as a very brief introduction to using RegEx methods in JavaScript - while the meta-characters and examples provided may help with very basic searches, it is likely that you will find yourself needing something more specific.
For further information, reference the MDN docs for regular expressions (It's what I referenced for this article!). For practice with regular expressions, RegExr is a great site to use.