Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference between test () and exec () methods in Javascript
The RegExp.prototype.test() and RegExp.prototype.exec() methods are JavaScript methods used to handle regular expressions. These methods provide different ways to search for patterns in strings and return different types of results.
What are regular expressions?
Regular expressions are patterns used to search for character combinations in strings. In JavaScript, regular expressions are treated as objects and are commonly referred to as "regex" or "RegExp."
The exec() Method
The exec() method searches for a match in a string and returns an array containing the match details if found, or null if no match is found. The returned array includes the matched string, captured groups, and additional information like the index position.
Syntax
regularExpressionObj.exec(string)
Example
Here's an example demonstrating the exec() method searching for patterns in a string:
<!DOCTYPE html>
<html>
<head>
<title>exec() - Regular Expression in JavaScript</title>
</head>
<body>
<p>Click to get exec() method output</p>
<button onclick="findMatch()">Search</button>
<p id="tutorial"></p>
<script>
function findMatch() {
var txt = "Learning regular expressions in JavaScript";
var search1 = new RegExp("JavaScript");
var search2 = new RegExp("Python");
var res1 = search1.exec(txt);
var res2 = search2.exec(txt);
document.getElementById("tutorial").innerHTML =
"Given string: " + txt + "<br>" +
"Search patterns: " + search1 + " and " + search2 + "<br>" +
"Results: " + res1 + " | " + res2;
}
</script>
</body>
</html>
The test() Method
The test() method searches for a match in a string and returns a Boolean value: true if the pattern is found, or false if not found. Unlike exec(), it only tells you whether a match exists without providing details about the match.
Syntax
regularExpressionObj.test(string)
Example
This example shows the test() method checking for pattern matches:
<!DOCTYPE html>
<html>
<head>
<title>test() - Regular Expression in JavaScript</title>
</head>
<body>
<p>Click to get test() method output</p>
<button onclick="findMatch()">Search</button>
<p id="tutorial"></p>
<script>
function findMatch() {
var txt = "Learning regular expressions in JavaScript";
var search1 = new RegExp("JavaScript");
var search2 = new RegExp("Python");
var res1 = search1.test(txt);
var res2 = search2.test(txt);
document.getElementById("tutorial").innerHTML =
"Given string: " + txt + "<br>" +
"Search patterns: " + search1 + " and " + search2 + "<br>" +
"Results: " + res1 + " | " + res2;
}
</script>
</body>
</html>
Key Differences Between exec() and test()
The main differences between these methods relate to their return values and use cases:
| Method | Return Type | Use Case | Performance |
|---|---|---|---|
exec() |
Array or null | When you need match details | Slower (more processing) |
test() |
Boolean | When you only need to check existence | Faster (less processing) |
Example Comparison
Here's a direct comparison showing both methods on the same regular expression:
<!DOCTYPE html>
<html>
<body>
<script>
var regex = RegExp("^([a-z]+) ([A-Z]+)$");
var testString = "hello WORLD";
var execResult = regex.exec(testString);
var testResult = regex.test(testString);
document.write("Result of exec(): " + execResult + "<br>");
document.write("Result of test(): " + testResult);
</script>
</body>
</html>
Result of exec(): hello WORLD,hello,WORLD Result of test(): true
Note that exec() returns an array where the first element is the complete match, followed by captured groups. The test() method simply returns true indicating a match was found.
Conclusion
Use test() when you only need to verify if a pattern exists in a string. Use exec() when you need detailed information about the match, including captured groups and position data.
