,你想做什麼瀏覽器? IE9 +可以使用TreeWalker
var t = document.createTreeWalker(document, NodeFilter.SHOW_COMMENT),
e;
while (e = t.nextNode()) { // iterate over comments
if (/\*g$/.test(e.nodeValue)) { // whatever test you want to do
console.log(e);
}
}
您可以實現沃克自己,這是我在otheranswers之前完成,但它可以變得複雜:(
開始寫的填充工具/墊片TreeWalker,document.createTreeWalker
和NodeFilter,很多丟失,但它是給你比我以上使用什麼需要更多的東西
if (!window.TreeWalker) {
window.TreeWalker = (function() {
function TreeWalker() {
// pass
}
return TreeWalker;
}());
}
if (!document.createTreeWalker) { // assuming TreeWalker
document.createTreeWalker = (function() {
var Constructor = TreeWalker;
try { // Illegal construction workaround
new Constructor();
} catch (e) {
Constructor = function TreeWalker() {};
Constructor.prototype = TreeWalker.prototype;
}
function nextNode() {
var e = this.currentNode;
if (e === null) {
e = this.root;
if (this.whatToShow & Math.pow(2, e.nodeType - 1))
return this.currentNode = e;
}
while (1) {
while (e.firstChild) {
e = e.firstChild;
if (this.whatToShow & Math.pow(2, e.nodeType - 1))
return this.currentNode = e;
}
while (!e.nextSibling && e.parentNode !== this.root) {
e = e.parentNode;
}
if (!e.nextSibling && e.parentNode === this.root) // reached end
return null; // none left
e = e.nextSibling;
if (this.whatToShow & Math.pow(2, e.nodeType - 1))
return this.currentNode = e;
}
}
function previousSibling() {
if (this.currentNode.previousSibling)
return this.currentNode = this.currentNode.previousSibling;
return null;
}
function nextSibling() {
if (this.currentNode.nextSibling)
return this.currentNode = this.currentNode.nextSibling;
return null;
}
function createTreeWalker(root, whatToShow, filter, entityReferenceExpansion) {
var t = new Constructor();
// root
t.root = root || document;
// whatToShow
if (whatToShow === 0)
t.whatToShow = 0;
else // -1 | 0
t.whatToShow = (whatToShow | 0) || 0xFFFFFFFF;
// todo: filter
t.filter = filter || null;
// todo: entityReferenceExpansion
t.entityReferenceExpansion = entityReferenceExpansion || null;
// currentNode
t.currentNode = root;
// nextNode
t.nextNode = nextNode;
// todo: previousNode
/* test for previousSibling before parentNode
* if previousSibling, keep doing lastChild until no more
* test against whatToShow
*/
// todo: parentNode
// todo: firstChild
// todo: lastChild
// previousSibling
t.previousSibling = previousSibling;
// nextSibling
t.nextSibling = nextSibling;
// return
return t;
}
return createTreeWalker;
}());
}
if (!window.NodeFilter) {
window.NodeFilter = (function() {
function NodeFilter() {
// pass
}
NodeFilter.FILTER_ACCEPT = 1;
NodeFilter.FILTER_REJECT = 2;
NodeFilter.FILTER_SKIP = 3;
NodeFilter.SHOW_ALL = -1;
NodeFilter.SHOW_ATTRIBUTE = 2;
NodeFilter.SHOW_CDATA_SECTION = 8;
NodeFilter.SHOW_COMMENT = 128;
NodeFilter.SHOW_DOCUMENT = 256;
NodeFilter.SHOW_DOCUMENT_FRAGMENT = 1024;
NodeFilter.SHOW_DOCUMENT_TYPE = 512;
NodeFilter.SHOW_ELEMENT = 1;
NodeFilter.SHOW_ENTITY = 32;
NodeFilter.SHOW_ENTITY_REFERENCE = 16;
NodeFilter.SHOW_NOTATION = 2048;
NodeFilter.SHOW_PROCESSING_INSTRUCTION = 64;
NodeFilter.SHOW_TEXT = 4;
return NodeFilter;
}());
}
如果你想匹配'* g'的字面意思,你需要跳過星號'\ * g',因爲在正則表達式中它是一個量詞 – HamZa
Hi HamZa。謝謝你的提醒。不過,我仍然遇到了錯誤。 –
你可以提供一個工作[小提琴](http://jsfiddle.net)?我不是javascript的專家,但它有助於調試您的問題。另外,你是否在'console.log(value.test(/(([\ s \ S])*?\ * g)/))'後忘記了一個分號';' – HamZa