Regex with Javascript

Regex stands for Regular Expressions also known as Regexp are special strings that represent a search pattern.

For programmers, Regex is like pills that make our task easy.

Regex contains special meaning for some characters that we will explain in further discussion during this post.

Now we will perform some action using functions of Regular Expression.

Match String Using test() method :

As we know that Regex is used to match parts of string.
So, if we want to find “foodie” in the string “Tommy is a foodie.” then we have to use /foodie/ as expression.

Now /foodie/ is our Regex.

Note: Quote marks are not required within regular expression.

JavaScript has multiple ways to use Regex. One of the ways is to use test() method which returns true or false.

Syntax :



regex.test(string)
//return true or false

Example :



var myString = "Codeteaser";
//if our search pattern is teaser from above String
var myRegex = /teaser/;
console.log(myRegex.test(myString));

OutPut:

Match String Literals

We can follow the same approach with String Literals as we have followed previously for matching a string pattern form a strong. In this case, I am taking the same example which I had discussed at the beginning of the post.

Example :


var myStringLterals = "Tommy is a foodie.";
//Search for foodie from string literal
var myRegex =/foodie/;
console.log(myRegex.test(myStringLterals));

OutPut:

But from this approach another form of foodie like Foodie, FOODIE will not match.

Example :


var myStringLterals = "Tommy is a foodie.";
//Search for foodie from string literal
var myRegex =/Foodie/;
console.log(myRegex.test(myStringLterals));

OutPut:

Searching Multiple Option at a time

If we have a string literal “Ronny has a pet cat.” and we are not sure about pet name i.e it is cat or dog or whatever it is. But if we have to add as search pattern on this specific string literal then, in this case, we can follow this approach as shown below.

Example :


var myStringLiteral ="Ronny has a pet cat."
// search for pet name but not sure about pet name
var myRegex = /dog|cat/
console.log(myRegex.test(myStringLiteral));

OutPut:

Note :

We can search multiple patterns using OR or | operator.

Ignore Cases while matching

During matching pattern in a string literal, we had faced a problem while match pattern remains in different cases i.e upperCase (“A”, ”B”, “C” … “Z”) or lowerCases (“a”,”b”,”c” … “z”).
As we had discussed foodie, Foodie or FOODIE are different patterns and these dissimilarities are due to letter case differences.

So, we can ignore letter cases in pattern match using flags in Regex. There are various flags in Regex and ignoring case we will use i flag.

Example:


var myStringLterals = "Tommy is a foodie.";
//Search for foodie from string literal using i flag
var myRegex =/Foodie/i;
console.log(myRegex.test(myStringLterals));

OutPut:

Extract Matches

So till now, we have worked on pattern is existing or not in a String or String Literal. Now we going to extract match pattern from string.
For that purpose, we have to use .match() method.

Example:



/**
* Extrat match pattern
*/

var myExtractStringLiteral="Selena Gomez is a pop Singer.";
// Looking for match pattern
var myRegex =/Singer/;
console.log(myExtractStringLiteral.match(myRegex));

OutPut:

Find more than first

match() method returns the first pattern from a string or string literal.

Example:



/**
* only find first pattern
*/

var myExtractStringLiteral = "Jonny Jonny yes papa";
// Looking for string pattern from a string literal
var myRegex =/Jonny/;
console.log(myExtractStringLiteral.match(myRegex));

OutPut:

But if a string Literal contains multiple times the same pattern then in that case for returning all string pattern we use the g flag as shown in the upcoming example.

Example:



/**
* return all match pattern from a string literal 
*/

var myExtractStringLiteral = "Jonny Jonny yes papa";
// Looking for all match pattern inside String Literal
var myRegex =/Jonny/g;
console.log(myExtractStringLiteral.match(myRegex));

OutPut:

Match anything with wildCard period

Sometimes we don’t know the exact pattern that we want to match. So, in that case, we have to try all possible words that we think for a matching pattern.
And i.e seriously a tough task.
But Regex contains wildCard character: . (dot)
The wildCard character will match anyone character of string.
The wildcard is also called a dot or period.
For example, if we have to match “hug”, “huh”, “hut” then our Regex pattern would be /hu./

Example:



/**
* wildCrad
*/


var myExtractStringLiteral = "I don't like fun during my working period.";
// Searching for fun
var myRegex =/fu./;
//After using dot it will match one character
console.log(myExtractStringLiteral.match(myRegex));

OutPut:

Note: We will discuss more about Regex in further posts.

Leave a Reply

Your email address will not be published. Required fields are marked *