jQuery:正则表达式测试注释节点

时间:2014-04-26 19:00:06

标签: javascript jquery regex testing nodes

我想测试评论是否以 * 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);

1 个答案:

答案 0 :(得分:1)

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

你可以自己实现一个助行器,这是我之前在other answers中所做的,但它可能会变得复杂:(


开始为 TreeWalker document.createTreeWalker NodeFilter 编写一个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;
    }());
}