JavaScript - How to Access Matched Groups in Regular Expression? Last Updated : 06 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Here are the different methods to access matched groups in JavaScript regular Expression(RegExp).1. Using exec() MethodThe exec() method returns an array with the entire match and captured groups, which you can access by their index in the result array. JavaScript let s = "The price is $50"; let regex = /(\d+)/; let res = regex.exec(s); console.log(res); console.log(res[1]); Output[ '50', '50', index: 14, input: 'The price is $50', groups: undefined ] 50 regex.exec(s) executes the regular expression on the string s.The first element of the result array is the full match, and result[1] gives the captured group (in this case, the digits).2. Using match() Method with Global FlagIf you omit the g flag, match() will return an array of matched groups along with the full match. JavaScript let s = "I have 10 apples and 20 oranges"; let regex = /(\d+)/g; let res = s.match(regex); console.log(res); Output[ '10', '20' ] s.match(regex) returns an array with all matches of the pattern (\d+) in the string s.Each element in the array is a separate match of the captured digits.3. Using match() Method Without the Global FlagWithout the g flag, match() gives you a detailed array with the entire match and captured groups. JavaScript let s = "My phone number is 12345"; let regex = /(\d+)/; let res = s.match(regex); console.log(res); console.log(res[1]); Output[ '12345', '12345', index: 19, input: 'My phone number is 12345', groups: undefined ] 12345 s.match(regex) matches the first occurrence of the pattern.The full match is at result[0] and the captured group is at result[1].4. Using String.prototype.replace() with a Callback FunctionThe replace() method allows you to access capturing groups through the callback function's parameters. JavaScript let s = "I have 100 dollars"; let regex = /(\d+)/; let res = s.replace(regex, (match, g1) => { console.log(g1); return `Amount: ${g1}`; }); console.log(res); Output100 I have Amount: 100 dollars s.replace(regex, callback) replaces the first match with the string returned by the callback function.The captured group (in this case, the number 100) is passed as group1 to the callback function.5. Using RegExp exec() MethodUsing exec() with a global flag in a loop allows you to retrieve all matches and access their groups. JavaScript let s = "I have 10 apples and 20 oranges"; let regex = /(\d+)/g; let match; while ((match = regex.exec(s)) !== null) { console.log(`Matched: ${match[0]}, Captured group: ${match[1]}`); } OutputMatched: 10, Captured group: 10 Matched: 20, Captured group: 20 The exec() method is used in a loop to find all matches of the regex pattern.match[0] is the full match, and match[1] is the captured group. Comment More infoAdvertise with us Next Article JavaScript - How to Access Matched Groups in Regular Expression? N nikhilgarg527 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-RegExp JavaScript-Questions Similar Reads JavaScript - How to Use a Variable in Regular Expression? To dynamically create a regular expression (RegExp) in JavaScript using variables, you can use the RegExp constructor. Here are the various ways to use a variable in Regular Expression.1. Using the RegExp Constructor with a VariableIn JavaScript, regular expressions can be created dynamically using 3 min read How to return all matching strings against a regular expression in JavaScript ? In this article, we will learn how to identify if a string matches with a regular expression and subsequently return all the matching strings in JavaScript. We can use the JavaScript string.search() method to search for a match between a regular expression in a given string. Syntax: let index = stri 3 min read How to Make Java Regular Expression Case Insensitive in Java? In this article, we will learn how to make Java Regular Expression case-insensitive in Java. Java Regular Expression is used to find, match, and extract data from character sequences. Java Regular Expressions are case-sensitive by default. But with the help of Regular Expression, we can make the Jav 2 min read JavaScript RegExp (Regular Expression) A regular expression is a special sequence of characters that defines a search pattern, typically used for pattern matching within text. It's often used for tasks such as validating email addresses, phone numbers, or checking if a string contains certain patterns (like dates, specific words, etc.).I 4 min read Convert user input string into regular expression using JavaScript In this article, we will convert the user input string into a regular expression using JavaScript.To convert user input into a regular expression in JavaScript, you can use the RegExp constructor. The RegExp constructor takes a string as its argument and converts it into a regular expression object 2 min read JavaScript RegExp [abc] Expression The RegExp [abc] Expression in JavaScript is used to search any character between the brackets. The character inside the brackets can be a single character or a span of characters.[A-Z]: It is used to match any character from uppercase A to Z.[a-z]: It is used to match any character from lowercase a 2 min read How to Search a String for a Pattern in JavaScript ? Here are two ways to search a string for a pattern in JavaScript.1. Using the string search() MethodThe JavaScript string search() method is used to search a specified substring within the given string and regular expression. It returns the index of the first occurrence of the pattern or -1 if the p 2 min read Properties of Regular Expressions Regular expressions, often called regex or regexp, are a powerful tool used to search, match, and manipulate text. They are essentially patterns made up of characters and symbols that allow you to define a search pattern for text. In this article, we will see the basic properties of regular expressi 7 min read Regular Expressions to Validate Google Analytics Tracking Id Given some Google Analytics Tracking IDs, the task is to check if they are valid or not using regular expressions. Rules for the valid Tracking Id are: It is an alphanumeric string i.e., containing digits (0-9), alphabets (A-Z), and a Special character hyphen(-).The hyphen will come in between the g 5 min read Perl - Use of Capturing in Regular Expressions A regular expression or a regex is a string of characters that define the pattern that we are viewing. It is a special string describing a search pattern present inside a given text. Perl allows us to group portions of these patterns together into a subpattern and also remembers the string matched b 3 min read Like