As said in the title, I'd like to find something like :contains()
but to match an exact string. In other words, it shouldn't be a partial match. For example in this case:
<div id="id">
<p>John</p>
<p>Johny</p>
</div>
$("#id:contains('John')")
will match both John
and Johny
, while I'd like to match only John
.
Thanks in advance.
EDIT: ES2015 one-liner solution, in case anyone looked for it:
const nodes = [...document.querySelectorAll('#id > *')].filter(node => node.textContent === 'John');
console.log(nodes);
/* Output console formatting */
.as-console-wrapper { top: 0; }
.as-console { height: 100%; }
<div id="id">
<p>John</p>
<p>Johny</p>
</div>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…