vladik3333,
...
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
</head>
<body>
<span class="test">текст1</span>
<span class="test">текст1</span>
<span class="test">текст2</span>
<span class="test">текст1</span>
<span class="test">текст2</span>
<span class="test">текст1</span>
<script>
let str = 'текст2';
let {length} = [...document.querySelectorAll("span.test")].filter(({textContent}) => textContent.includes(str));
alert(length);
</script>
</body>
</html>