Regex & Oop
Regex & Oop
Intermediary…
JavaScript Regular Expressions
In programming, we frequently parse or make queries over strings. Regular
expressions are expressions containing a sequence of characters to invoke a sequence
matching or pattern matching of strings.
Regex for Email & Password:
For strings taken as input, say goodbye to the list of conditionals and string
methods that make sure a string is correctly input.
Pattern matchings become tedious when writing string
manipulation or iteration code. Moreover, it is not always
best to write loops to index and iterate strings for
manipulation.
The solution to these issues? Regular expressions.
Syntax - Brackets
Brackets, or square brackets [], are patterns that find a range of characters in the string. The brackets
encapsulate the criteria or pattern that the string needs to satisfy.
Syntax - Quantifiers
Quantifiers are special characters or flags that specify the position or frequency of a certain pattern. Look at
these quantifiers and their different connotations. Do note that p is any pattern in the following table.
Syntax - Lookaround
Lookarounds are assertions that see if a character match is possible or not, and accordingly return boolean
values without consuming any part of the string. p and q are any patterns in the following table.
Syntax - Character Literals
Character literals are representations for characters and special characters. They make patterns more flexible.
Check out some of the commonly available character literals
Syntax - Metacharacters
Metacharacters are character representations for a group of characters falling under the same categories. They
help make patterns simple. Metacharacters let us avoid listing all possible characters for a pattern.
Regex in JS - Modifiers
Modifiers are additional flags that allow case-incentive, global, and multiline matches. They are not a part of the
pattern, but a flag for RegEx itself. Here is a list of modifiers.
Exercise
Password Checker
Task:
A software engineer is busy developing an app for a startup.
Currently, she is working on the login and sign up screen. An integral
part of this screen is ensuring that the user selects a strong
password. Because she is already very busy, you have been
assigned to implement a function that can tell if a given password is
or is not strong.
Problem Statement:
You are given a password variable as an argument, which is assigned to a
string representation of the password. Complete function isSafe so that it
will confirm if the password meets the following criteria:
• Length: The length of the password is greater than or equal to six characters.
• Uppercase letter: The password contains at least one uppercase alphabet (A-Z).
• Lowercase letter: The password contains at least one lowercase alphabet (a-z).
• Numeric digit: The password contains at least one digit (0-9).
• Special Character: The password contains at least one special character (any character other than
{0-9, a-z, A-Z}).
Given the above factors, use RegExes and its test method to return a boolean if true if the password
meets the above criteria. It will return false if it does not.
JavaScript
Intermediary…
JavaScript OOP
If we want to create a custom class of the object or do other fun things, we need object-oriented programming.
Illustration:
OOP in JS:
• Prototype-based Programming: In this programming style,
the object encapsulates the properties, its methods and data, instead of
a class. You can add new properties to this object whenever you want.
An object can be an individual, instead of an instance of the class; if
you want an object, you can create one without creating a class first.
• Class-based Programming: The class-based programming allows
using the class token in implementation. Furthermore, they assist
in inheritance and in implementing OOPs through constructor, extend, super,
and static keywords. This new syntax is clearer and easier to use.
JavaScript Prototypes
All JavaScript objects have the property prototype. They go under the property name __proto__
JavaScript Constructor Functions
Constructor functions, or object constructor functions, contain blueprints to a type of object that
has properties and methods to generate objects.
Syntax:
All objects created from a constructor function will have the same properties and methods but not necessarily the
same values.
JavaScript Classes
Similar to constructor functions, classes are blueprints of a type of object with the certain properties
and methods to generate objects.
Syntax:
Data Protection
At times, it is important for properties to be hidden or stay inside of an object.
Accessibility:
Inheritance
Inheritance in OOPs is the ability for a class to take properties of another class.
Illustration:
A class can inherit from any number of classes:
JavaScript Object Notation
JSON (JavaScript Object Notation) is a lightweight text-based language that helps store data.
Converting JSON
• JSON.parse method
• JSON.stringify method
Exercise
Modelling Classes
Task:
An architect is designing a 3D model for a new building. Given the
many shapes that make up a building, he wants to calculate the
area of his custom shapes. For now, he wants you to create a
class or constructor function called which can both calculate
Rectangle
the area of his walls and also tell if a wall is squarish. Because
there are lots of shapes, you will also be creating a class or
constructor function for all shapes named . This will store
Shape
the class.
Shape
Problem Statement:
Implement classes or constructor functions for Shape and Rectangle where
the Rectangle class/constructor function will inherit from the Shape class/
constructor function.
Shape class/constructor function
The Shape class/constructor function will have only
one public method responsible for getting a shape’s
name: getName. The name of the shape should not be accessed
directly because we don’t want to modify the name of the shape
after creation. Use a data protection technique. While creating
a Shape object, only the name of the shape is passed.
Problem Statement:
Implement classes or constructor functions for Shape and Rectangle where
the Rectangle class/constructor function will inherit from the Shape class/constructor
function.
Rectangle class/constructor function
The Rectangle class/constructor function will be derived from the Shape class/
constructor function. It will have publicly accessible
properties side1 and side2. Moreover, the class/constructor function will
have two methods: getArea and isSquare. These methods calculate the area
and tell if the shape is or is not a square. While creating a Rectangle object,
the name, side1, and side2 are passed.
Guide:
Reminder, rules so far
1. Think before you program!
2. A program is a human-readable essay on problem solving that also happens to
execute on a computer.
3. The best way to improve your programming and problem-solving skills is to
practice!
4. A foolish consistency is the hobgoblin of little minds
5. Test your code, often and thoroughly
6. If it was hard to write, it is probably hard to read. Add a comment.
7. All input is evil, unless proven otherwise.
8. A function should do one thing.