Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery:Regex测试注释节点_Javascript_Jquery_Regex_Testing_Nodes - Fatal编程技术网

Javascript jQuery:Regex测试注释节点

Javascript jQuery:Regex测试注释节点,javascript,jquery,regex,testing,nodes,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)) } }); 但

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

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

   $('body', $content).contents().each(function() {
        if(this.nodeType == 8 ){
            var value = this.nodeValue;
            console.log(/(([\s\S])*? \*g)/.test(value))
        }
    });
但是,我得到了
uncaughttypeerror:undefined在测试中不是一个函数
错误

我非常感谢你的帮助


已解决:

问题:测试模式不正确


正确的模式:
regexp.test(字符串)

您的目标浏览器是什么?IE9+可以使用

您可以自己实现一个助行器,我以前在中做过,但它可能会变得复杂:(


开始为TreeWalker编写polyfill/shim,
文档。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
,因为在正则表达式中它是一个量词。谢谢你的提醒。不过我还是会出错。你能提供一个工作表吗?我不是javascript专家,但它会帮助你调试问题。还有,你忘了分号
console.log(value.test(/([\s\s])*?\*g)/)
?是的。但是在JS中它不是必需的。这里有一个小问题:我将把它用于商业插件,所以我更喜欢IE8工作的解决方案。不过,还是谢谢你的建议!哇!太棒了。谢谢!
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;
    }());
}