matches function in javascript code example
Example 1: how to use the match function in javascript for regex
const str = 'For more information, see Chapter 3.4.5.1';
const re = /see (chapter \d+(\.\d)*)/i;
const found = str.match(re);
console.log(found);
Example 2: matches method in javascript
<ul id="birds">
<li>Orange-winged parrot</li>
<li id="h" class="endangered">Philippine eagle</li>
<li>Great white pelican</li>
</ul>
<script type="text/javascript">
var birds = document.getElementsByTagName('li');
for (var i = 0; i < birds.length; i++) {
if (birds[i].matches('#h')) {
console.log('The ' + birds[i].textContent + ' is endangered!');
}
}
</script>