/

HTML / CSS / JavaScript Tutorial

JavaScript regular expression method: exec()

[this page | pdf | back links]

The exec() method (when applied to a JavaScript regular expression) seeks a match in a string and returns the first match.

 

It has the following syntax with the following parameters. It returns a string as above, or null if no such string is found.

 

RegExprObject.exec(string)

 

Parameter

Required / Optional

Description

string

Required

The string to be searched

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
var str = "Hello World";
var patt = /o/gim;
document.getElementById("Example").innerHTML =
  'var str = "Hello WORLD";' + '<br>' +
  'var patt = /o/gim;' + '<br>' +
  'var x = patt.exec(str);'
document.getElementById("Result").innerHTML = patt.exec(str);
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Regular Expressions


Desktop view | Switch to Mobile