jQuery:Regex测试一个注释节点

jQuery: Regex testing a comment node

本文关键字:一个 注释 节点 Regex 测试 jQuery      更新时间:2023-09-26

我想测试注释是否以*g结尾,这是我正在研究的删除多余注释的特定模式。

以下是我到目前为止所拥有的:

   $('body', $content).contents().each(function() {
        if(this.nodeType == 8 ){
            var value = this.nodeValue;
            console.log(/((['s'S])*? '*g)/.test(value))
        }
    });

然而,我在测试中得到了Uncaught TypeError: undefined is not a function错误。

我很感激你的帮助。


已解决:

问题:测试模式不正确。

正确模式:regexp.test(string);

您的目标浏览器是什么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);
    }
}

你可以自己实现助行器,我以前在其他答案中也这样做过,但它可能会变得复杂:(


开始为TreeWalkerdocument.createTreeWalkerNodeFilter编写polyfill/shim,缺少很多内容,但它为您提供了超出我在上使用的内容所需的内容

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;
    }());
}