Logo
Unit 7 – Regular Expressions

Regular Expressions

Duration: 8 minutes

Greetings, code enthusiasts! Embarking on the journey of coding often leads to encounters with patterns, especially when dealing with text. In the programming world, there’s a powerful tool designed just for this: Regular Expressions, often shortened to RegExp. They provide a way to match strings of text, such as particular characters, words, or patterns of characters. Let’s delve into the intricate yet fascinating world of RegExp!

Understanding Regular ExpressionsBasic Syntax:

Regular expressions are patterns enclosed between slashes (/). For instance, /pattern/flags. Flags help with the type of matching you want to achieve:- g (global) - Find all matches.

  • i (case-insensitive) - Matches are case-insensitive.
  • m (multiline) - Allows start and end characters (^ & $) to work over multiple lines. Special Characters: There are many special characters in RegExp, like . which matches any character (except for a newline), * which matches the preceding element zero or more times, + which matches one or more times, etc. Basic example:
const regex = /ab*c/; // This matches any string containing an "a", followed by zero or more "b"s, and a "c".

Character Sets and Classes: You can match any one of a set of characters using square brackets [], or use a dash - for a range of characters. Basic example:

const regex = /[A-Z]/; // This matches any single uppercase letter.

Exercise

Time to apply your newfound knowledge:- Create a simple form that takes a user’s email as input.

  • Validate this email using a regular expression to ensure it fits the typical format: localpart@domain. com.
  • Display a message to the user indicating if their email is valid or not. Hints for the exercise:- Use the RegExp. prototype. test() method to test the user input against your regular expression.
  • A very basic regular expression pattern for an email could look like /^[a-zA-Z0-9. _-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,6}$/. However, be aware that email validation can get much more complex, and this is a simplified version. Sample code to start with:
const emailRegex = /^[a-zA-Z0-9. _-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
function validateEmail(email) {
  if (emailRegex.test(email)) {
    console.log("Valid email!");
  } else {
    console.log("Invalid email!");
  }
}
// Sample usage:
validateEmail("example@email. com"); // Outputs: Valid email!
validateEmail("example-email"); // Outputs: Invalid email!

Conclusion

A round of applause for diving into the deep and dynamic domain of regular expressions! They’re an immensely powerful tool in any developer’s arsenal, enabling intricate pattern matching and text manipulation. While they might seem complex initially, with practice, they become an invaluable asset. Continue to experiment, explore, and excel with RegExp as you traverse the vast realm of coding. Keep it up!

Next Tutorial: Modules and Packages

5 minutes Minutes

Continue

Code on the Go with our Mobile App!

Unleash your coding potential anytime, anywhere!

Download Now!