Javascript Regular Expressions
So this is our last post on Javascript Course. In that, we are going to see, Regular Expressions in Javascript.
What is a Regular Expression?
JavaScript regular expressions (regex) are powerful tools for pattern matching and manipulating strings. They allow you to search, replace and extract information from text using a set of characters and operators to define a pattern. In this blog post, we will explore the basics of JavaScript regular expressions and show you how to use them effectively.
It is basically a sequence of characters, which is used to make a search or replace operations in webpages.
Syntax :
/pattern/modifiers;
Example :
const = /Hello/;
So this a simple example of Regular Expression, which matches the word "Hello".
Matching Characters Using RegExp
Regular Expressions allow users to match specific characters or sets of characters.
For example, a block of code defines a paragraph and we want to search or replace some characters from this paragraph, then at that time, we can use RegExp to make matches.
const regex = /r/;
We can also match a range of characters using a hyphen (-) between two characters.
For example, we have to find the characters between 'e' to 'h', then the pattern is,
const regex = /[e-h]/;
This pattern will search the matches which contain the characters between 'e' and 'h'.
The patterns follow the rule of case sensitivity, which means when the user provides any pattern which will contain the lowercase characters, then the pattern finds the matches which will contain only the range of lowercase or simply a single lowercase character.
For Example,
const regex1 = /[a-e]/;
const regex2 = /[A-E]/;
There is another way to find the matches of all the Lowercase and Uppercase characters.
const regex = /[a-Z]/;
You can also use special character classes to match common sets of characters. Here are a few examples:
- '\d' matches any digit (0-9).
- '\w' matches any word character (a-z, A-Z, 0-9, and underscore).
- '\s' matches any whitespace character (space, tab, newline).
For Example,
const regex1 = /^\d/; // It will finds Number 0-9
const regex2 = /^\w/; // It will finds any words a-z, A-Z, 0-9
const regex3 = /^\s/; // It will finds spaces, tabs, and newlines
So these are the ways to make matches or find the matches using RegExp.
Modifiers
In regular expressions (RegExp) in JavaScript, modifiers are special characters that are added to the end of a pattern to modify how the pattern is matched. Modifiers allow you to perform case-insensitive matching, global matching, and more.
Modifiers | Description | Try It |
---|---|---|
i | It is used to perform case-insensitive matching | |
g | It is used to perform Global Matches | |
m | It is used to perform Multiline matches |
Quantifiers
Regular expressions also allow you to specify how many times a character or set of characters should be matched using quantifiers. Here are a few examples:
Quantifiers | Description | Try Now |
---|---|---|
'*' | It is used to find the matches, that contain zero o more occurrences |
|
'+' | It is used to find the matches, that contains one o more occurrences |
|
'?' | It is used to find the matches, that contain zero o one occurrence |
Methods of RegExp:
Regular expressions (RegEx) in JavaScript can be created using the RegExp() constructor or by using forward slashes / / to enclose the pattern. Once a RegEx object is created, there are several methods available to work with it.
There 5 main methods are provided for RegExp :
- test()
- exec()
- match()
- search()
- replace()
All of these methods are used to perform specific tasks according to, their usage.
1. test() :
This method is used to check whether, a given string matches the RegExp pattern or not, and returns true or false.
Suppose, there is a string that contains some numerical values, then by using this method we can able to check, whether the given string contains any numbers or not.
Syntax :
pattern.test(String/StringObj);
Example :
let text = "My Roll Nnumber is 4, and I'm in 3rd year of Computer Engineering";
const regex = /[0-9]/g;
let result = regex.test(text);
if(result == true)
{
document.getElementById("Demo").innerHTML = "Yes";
}
else
{
document.getElementById("Demo").innerHTML = "No";
}
2. exec() Method :
This method of RegExp is used to search a specified pattern in the given string and returns the founded text as an object.
Suppose There is a String that will contain some Numbers, then by using this method, we can find out, which numbers contain this string.
Syntax :
pattern.exec(String/StringObj);
Example :
let text = "My Roll Nnumber is 4";
const regex = /[0-9]/g;
let result = regex.exec(text);
document.getElementById("Demo1").innerHTML = result;
3. match() Method :
This method searches a string for a match against a RegEx pattern and returns an array of matches.
Suppose, there is a string that contains the name of CEO Rohan Kumar Bhoi, then this method of RegExp will help us to find the name of the CEO from the given String.
Syntax :
String/StringObj.match(pattern);
Example :
let text = "CEO and Founder of CompTT is Mr. Rohan Kunar Bhoi, Mr. Rohan is a Fantastic guy ever !";
const regex = /rohan/i;
let result = text.match(regex);
document.getElementById("Demo2").innerHTML = result;
4. search() Method :
This method is used to search a string for a specified value and returns the index position of the first match.
Suppose, there is a string that contains "CompTT", then this method helps us to find the index position of "CompTT".
Syntax :
String/StringObj.search(value/pattern);
Example :
let text = "Short form of Computer Tips Tricks.tech is 'CompTT' given by CEO of 'CompTT'";
const regex = /CompTT/i;
let result = text.search(regex);
document.getElementById("Demo3").innerHTML = result;
5. replace() Method :
This method searches a string for a specified value or RegEx pattern and returns a new string with the matches replaced by a specified replacement string.
It means, this method will replace a specified value with another value in the string.
Suppose, there is a string, that contains the word 'Laptop' and we want to replace it with 'Computer', then this method will help to replace the word.
Syntax :
String/StringObj.replace(pattern/Value,"another value");
Example :
let text = "John said Laptop PCs are required to learn Programming !";
const regex = /laptop/i;
let result = text.replace(regex,"Computer");
document.getElementById("Demo3").innerHTML = result;
So these are the Methods of RegExp (Regular Expressions) of Javascript.
I hope this post will help you to learn Javascript More...!!
Thank You..!!!
To Claim Your Certificate, please go back and Click on Claim Certificate Button.
After that, fill out the Form which will open after clicking on the button and we will send you your certificate to your email.
Note: Provide the Correct Email ID, because the certificate will be sent to your Email Id.
Learn Programming with CompTT and Stay Connected with us..! ❤️
0 Comments
Please do not add Any Spam link in Comments !