Regex and Javascript

Franklyn Rodriguez
3 min readOct 21, 2020

Test and Match

I wanted to look into Regex as sometimes when I look at it, I feel that there is so much going it’s hard to comprehend. What I will be doing here is trying to break down the different aspects of Regex at more of a face level. I also wanted to get a better understanding of Regex so I can create better validations for the future projects/apps especially as things start to scale up.

Before I started talking about Regex I would like to offer a description of it from the MDN docs

Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec() and test() methods of RegExp, and with the match(), matchAll(), replace(), replaceAll(), search(), and split() methods of String. This chapter describes JavaScript regular expressions.

source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Being that there are a lot of pieces to Regex I have decided to create a mini series for this. The first two topics that I will be talking about here are the .test() method and .match() method.

.test() method

The test()method is used to take a regular expression and see if that Regex is in in that string. It returns a binary response of whether or not it it exists in that expression.

let myString = “Hello, World!”; 
let myRegex = /Hello/;
let result = myRegex.test(myString)
//returns true

With the test() method we take the Regex and test it out with the string and check out whether it’s true or false.

This is the opposite of how it works with the match()method.

'string'.match(/regex/); 
/regex/.test('string');

.match() method

The match() is used to find and extract out that regex from the string. It returns the matches found.

"Hello, World!".match(/Hello/); 
// Returns ["Hello"]
let ourStr = "Regular expressions";
let ourRegex = /expressions/;
ourStr.match(ourRegex);
// Returns ["expressions"]

The benefits of regex that comes to mind is for validating inputs on your website, as mal intent users may try to inject some type of SQL into your site to try and manipulate your database. It also helpful for making sure information put onto a contact form or sign up form is what you actually need and not just some random piece of information.

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$

Throughout this Regex series I will do my best to break down these character sets .

Subtle wink wink

I am also open to network and looking for new opportunities. If you have any projects you are working and are looking for someone to help you feel free to reach out to me. Also if you have any comments or advice you would like to give my contact information is posted below. See below .

If you would like to connect with me here is my Linkedin.
If you would like to see some of my work: github.

Thanks for reading!

.match() method

--

--

Franklyn Rodriguez

Full-Stack Developer on the search for new opportunities to learn and grow my skills