DEV Community

Black-Thor
Black-Thor

Posted on • Edited on

[week1] Days 3 - Regex ! part 2

Image complete regex

Busy day !

Today was a complicated day, I managed to work on my regex course, but in a rush that didn't allow me to fully understand the lessons I was learning.

So, to test myself tomorrow, I'm going to realise a mini challenge which consists in the realisation of an application that will check if a password corresponds to the standard of a secure password.

In advance sorry, but the rest of the article is from my personal notes, I do not have the time to edit everything.
But I will detail my creation tomorrow

What did i learn ?

Lazy Matching

let text = "<h1>Winter is coming</h1>";
let myRegex = /<.*?1>/;
let result = text.match(myRegex);//h1
Enter fullscreen mode Exit fullscreen mode

Match Beginning String Patterns

let rickyAndCal = "Cal and Ricky both like racing.";
let calRegex = /^Cal/; 
let result = calRegex.test(rickyAndCal);//Cal
Enter fullscreen mode Exit fullscreen mode

Match Ending String Patterns

let caboose = "The last car on a train is the caboose";
let lastRegex = /caboose$/; // Change this line
let result = lastRegex.test(caboose);//True
Enter fullscreen mode Exit fullscreen mode

Match All Letters and Numbers

/\w/gi 
Enter fullscreen mode Exit fullscreen mode


used to count all alphanumeric characters

Character classes

    - \w word \d digit \s whitespace (tabs, line breaks)
    - \W NOT word \D NOT digit \S NOT whitespace
    - \t tabs, \n line breaks
    - . any character (except newline)
Enter fullscreen mode Exit fullscreen mode

Specify Upper and Lower Number of Matches

let ohStr = "Ohhh no";
let ohRegex = /Oh{3,6}\sno/gi; // Change this line
let result = ohRegex.test(ohStr);//true 
Enter fullscreen mode Exit fullscreen mode

Specify Only the Lower Number of Matches

/ha{3,}h/
Enter fullscreen mode Exit fullscreen mode

this will match with haaaaaaah not hah

Specify Exact Number of Matches
/a{3}/

Check for All or None

let favWord = "favorite";
let favRegex = /favou?rite/; 
let result = favRegex.test(favWord) // favourite true also 
Enter fullscreen mode Exit fullscreen mode

Positive 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. A positive lookahead is used as (?=...) where the ... is the required part that is not matched.

  • On the other hand, a negative lookahead will look to make sure the element in the search pattern is not there. A negative lookahead is used as (?!...) where the ... is the pattern that you do not want to be there. The rest of the pattern is returned if the negative lookahead part is not present.

let sampleWord = "astronaut";
let pwRegex = /(?=\w{6})(?=\w*\d{2})/; 
let result = pwRegex.test(sampleWord); // match passwords that are greater than 5 characters long, and have two consecutive digits.
Enter fullscreen mode Exit fullscreen mode

Check For Mixed Grouping of Characters

let myString = "Eleanor Roosevelt";
let myRegex = /(Franklin|Eleanor).*Roosevelt/;
let result = myRegex.test(myString);// Eleanor Roosevelt and Franklin Roosevelt both true 
Enter fullscreen mode Exit fullscreen mode

Reuse Patterns Using Capture Groups

let repeatNum = "42 42 42";
let reRegex = /^(\d+)\s\1\s\1$/; 
let result = reRegex.test(repeatNum);//find the patern
Enter fullscreen mode Exit fullscreen mode

Remove Whitespace from Start and End

let hello = "   Hello, World!  ";
let wsRegex = /^\s+|\s+$/g; 
let result = hello.replace(wsRegex, ""); 
Enter fullscreen mode Exit fullscreen mode

PS:

  • if you see any spelling or grammar mistakes, can you notify me (I am still improving my English) ? thank you !
  • if you have any tips for improving my post feel free to comment on the post

Top comments (0)