JavaScript RegExp exec() Method Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 2 Likes Like Report The RegExp.exec() method in JavaScript allows you to search for a pattern in a string and retrieve detailed information about the match. Unlike simple methods like test(), exec() returns not just a boolean, but an array containing the entire match, capturing groups, the position of the match, and more. JavaScript let regex = /(\d{2})-(\d{2})-(\d{4})/; let s = "Today's date is 12-09-2023."; let res = regex.exec(s); if (res) { console.log("Full match:", res[0]); console.log("Day:", res[1]); console.log("Month:", res[2]); console.log("Year:", res[3]); } else { console.log("No match found."); } OutputFull match: 12-09-2023 Day: 12 Month: 09 Year: 2023 Regular Expression: /(\d{2})-(\d{2})-(\d{4})/ matches a date in the format dd-mm-yyyy. The parentheses () are used to capture groups.exec() method: Searches the string for the pattern and returns an array with detailed match information:res[0]: The full matched string.res[1], res[2], res[3]: The captured groups (day, month, and year).If no match is found, exec() returns null.Syntax:let result = regex.exec(string);regex: The regular expression to search for in the string.string: The string to search within.result: An array of match details if a match is found, or null if no match is found.Now let's see some uses of RegExp exec() Method1. Extracting Email ComponentsLet’s use exec() to extract the domain from an email address: JavaScript let regex = /@([a-zA-Z0-9.-]+)/; let mail = "[email protected]"; let res = regex.exec(mail); if (res) { console.log("Domain:", res[1]); } else { console.log("No domain found."); } OutputDomain: example.com 2. Finding All Digits in a StringYou can use exec() with the global flag (g) to find all occurrences of digits in a string JavaScript let regex = /\d+/g; let s = "I have 2 apples and 15 bananas."; let res; while ((res = regex.exec(s)) !== null) { console.log("Found:", res[0], "at position:", res.index); } OutputFound: 2 at position: 7 Found: 15 at position: 20 Key Points About exec()Detailed Match Information: exec() provides detailed information about the match, including the matched string, capture groups, the match's index in the input string, and the original input.Global Flag (g): When used with the g flag, exec() can find all matches in the string, returning results in a loop.Returns null on No Match: If no match is found, exec() returns null. Create Quiz Comment V Vishal Chaudhary 2 Follow 2 Improve V Vishal Chaudhary 2 Follow 2 Improve Article Tags : JavaScript JavaScript-RegExp JavaScript-Methods Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like