JavaScript regular expression method: test()
[this page | pdf | back links]
The test() method (when applied to a JavaScript
regular
expression) seeks a match in a string and returns true if found, otherwise false.
It
has the following syntax with the following parameters. It returns a Boolean
as above.
RegExprObject.test(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.test(str);'
document.getElementById("Result").innerHTML = patt.test(str);
</script>
</body>
</html>
|
NAVIGATION LINKS
Contents | Prev | Next | JavaScript Regular Expressions