Wednesday, July 3, 2024
HomeLanguagesJavascriptHow to Access Matched Groups in a JavaScript Regular Expression ?

How to Access Matched Groups in a JavaScript Regular Expression ?

In JavaScript regular expressions, parentheses are used to define capturing groups. Capturing groups allows to extract specific parts of the matched string. When a regular expression is executed, the matched groups can be accessed through the resulting match object.

In this article, we will learn how to access the matched groups in a JavaScript regular expression. There are two approaches to access the matched groups, these are:

  • Using exec() Method
  • Using match() Method

Approach 1: Using exec() Method

For searching a pattern if it is present then returns the entire string otherwise returns an empty string.

const regex = /pattern(g1)(g2)/; 
const match = regex.exec(inputString);

The matched groups can be accessed using numeric indexes. The index 0 represents the entire matched string, and subsequent indexes represent the captured groups.

Example: Extracting parts of a date

Javascript




const regex = /(\d{2})-(\d{2})-(\d{4})/;
const inputString = '27-06-2023';
const match = regex.exec(inputString);
  
const day = match[1];
const month = match[2];
const year = match[3];
  
console.log(day);  
console.log(month);
console.log(year);


Output

27
06
2023

Approach 2: Using match() Method

For searching a pattern if it is present then it returns true otherwise returns false.

const regex = /pattern(g1)(g2)/;
const match = inputString.match(regex);

Example: Parsing a URL:

Javascript




const regex = /(https?):\/\/([^:/\s]+)(:\d{2,5})?(\/[^\s]*)?/;
const inputString = 
const match = regex.exec(inputString);
  
const protocol = match[1];
const domain = match[2];
const port = match[3];
const path = match[4];
  
console.log(protocol);  
console.log(domain);    
console.log(port);      
console.log(path);


Output

https
www.neveropen.com
:8080
/path/to/resource

Conclusion: To access the matched groups in a JavaScript regular expression, we can use the exec() method or the match() method. Capturing groups defined using parentheses () allows for the extraction of extract specific parts of the matched string. Once there is a match object, we can access the matched groups using array indexes.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments