/

HTML / CSS / JavaScript Tutorial

JavaScript String method: match()

[this page | pdf | back links]

The match() method (when applied to a JavaScript string) searches the string for regular expression matches, and returns them as an array. If the regular expression does not include a g modifier (corresponding to a global search), match only returns the first match. If no match is found then the method returns null.

 

It has the following syntax with the following parameters:

 

string.match(regexpression)

 

Parameter

Required / Optional

Description

regexpression

Required

Regular expression being matched

 

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 y = "abcdeCD";
var regexp = /cd/ig;
document.getElementById("Example").innerHTML =
  'var y = "' + y + '";<br>' +
  'var regexp = /cd/ig;<br>' +
  'var x = y.match(regexp);';
document.getElementById("Result").innerHTML
  = y.match(regexp);
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodStringMatch() {
  var z = "abc"; return !!z.match;
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript String Variables


Desktop view | Switch to Mobile