Important Regular Expressions Every Developer Must Know

Regular expressions are a small but powerful language that can be used to find patterns in strings. These patterns can then be used to perform tasks on the strings or extract information from them.

Regular expressions are commonly used in many programming languages, including JavaScript, Python, Ruby and C# to name just a few. They are also used in many popular text editors such as Sublime Text 3 and Atom.

This post covers some of the most important regular expression techniques that every developer must know.

Back References in Regex (JavaScript)

  • You can use backreferences to match the same text again. To do this, you place the contents of the capture group inside
  • A pair of parentheses. You can then use the backslash () followed by the number of the capture group to match the same text again.

let repeatStr = "regex regex";

let repeatRegex = /(\w+)\s\1/;
repeatRegex.test(repeatStr); // Returns true
repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]

Using the .match() method on a string will return an array with the string it matches, along with its capture group.

Use the .test() method to test the regex and the .match() method to get the value of the capture group.

Use capture groups in regex to match numbers that are repeated only three times in a string, each separated by a space.

let repeatNum = "42 42 42";
let regex = /^(\d+)\s\1\s\1$/; 
let result = reRegex.test(repeatNum);
result;

Lookaheads

Lookaheads are patterns that tell JavaScript to look-ahead in your string to check for patterns further along. This can be useful when you want to search for multiple patterns over the same string.
There are two kinds of lookaheads: positive lookahead and negative lookahead.

  • A positive lookahead will look to make sure the element in the search pattern is there, but won’t actually match it.
  • /On the other hand, a negative lookahead will look to make sure the element in the search pattern is not there.
  • A positive lookahead is used as(?=...) where the … is the required part that is not matched.
  • A negative lookahead is used as (?!...) where the … is the pattern that you do not want to be there.
let quit = "qu";
let noquit = "qt";
let quRegex= /q(?=u)/; //Postivie lookahead
let qRegex = /q(?!u)/; //Negative lookahead
quit.match(quRegex); // Returns ["q"]
noquit.match(qRegex); // Returns ["q"]

More Examples

A more practical use of lookaheads is to check two or more patterns in one string. Here is a (naively) simple password checker that looks for between 3 and 6 characters and at least one number:

let password = "abc123";
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
checkPass.test(password); // Returns true

Use lookaheads to match passwords that are greater than 5 characters long, and have two consecutive digits

let sampleWord = "astronaut";
let passwordRegex = /(?=\w{5,})(?=\D*\d{2})/; // Change this line
let result = passwordRegex.test(sampleWord);

How to named the capture group

If you want to named the capture group then you can used the syntax (?<first>\d{3}) here the group named will be first. for example

let phoneNumber="123-456-123";
let matchedGroup=phoneNumber.match(/(?<first>\d{3})-(?<second>\d{3})-(?<third>\d{3})/);

//{ first: '123', second: '456', third: '123' }
console.log(matchedGroup!.groups);

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post