Select HTML Comments like any other DOM Node, with this fast comment selector implementation.
yarn add querycomments
NPM
npm install querycomments
If you don't use a package manager, you can access querycomments
via unpkg (CDN), download the source, or point your package manager to the url.
<!-- This is a comment -->
JSFiddle Example DEMO
getComments(context:DOMNode, filter:function|RegExp):Array
get all comments
getComments();
get all comments inside a element
getComments(document.getElementById('someDiv'));
get the first comment that match a regex pattern
getComments(document, /hey/i);
get the first comment is filtered by a function
getComments(document, (text, comment) => {
this.breakOnFirst = true; //I only need the first comment
return text.substring(0, 4) === 'Lorem'; //does it starts with "Lorem"
});
get All comments that match a regex pattern
getComments(document, /foo/g); // notice the global flag on the regex
get all comments filtered by a function
getComments(document, (text, comment) => {
const possibleValues = ['foo', 'bar', 'baz'];
for (var i = 0; i < possibleValues.length; i++) {
if (text.indexOf(possibleValues[i]) > -1) return true;
}
return false;
});
File a issue on this repository