javascript - jQuery:正则表达式测试评论节点

标签 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);

最佳答案

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

你可以自己实现一个walker,我之前在other中做过。 answers但它可能会变得复杂 :(


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

关于javascript - jQuery:正则表达式测试评论节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23315259/

相关文章:

javascript - 未捕获的类型错误 : Cannot read property 'addPoint' of undefined ( Highstock live data)

javascript - Android 版 Chrome - 不显示通知

jQuery 在函数之间切换 - 执行零、一、二、三...次

javascript - 使用正则表达式检查字符串和整数的组合

python - 在 Markdown 中取消/注释掉 html 标签的巧妙方法

javascript - 正则表达式不允许在字符串开始或结束处出现 '.' 、 '_' 、 '-' ,并且不应有连续的 '.' ,其余所有特殊字符都不应被允许

asp.net - Javascript 文件被 chop

javascript - 使用 javascript/jquery 单击下一个和上一个直到固定阶段来显示 div 或内容

javascript - 方法调用时的 meteor 表演微调器

php - 如何在提交时关闭 fancybox...请需要帮助