<!--<script type"text/javascript">
asdsd
</script>-->下边这个正则不管用字符串 = 字符串.replace(new RegExp("<!--(<script.*>[.\n\r]*<\/script>)-->", "ig"), "$1");
alert(字符串);正则高手帮忙看下,谢谢……

解决方案 »

  1.   

    <!--(<script.*>[\s\S]*<\/script>)-->
      

  2.   


    /*
     * 首页通公共类 对话框组件
     *
     * 插件盒子:plugins
     * 注册名称:dialog、对话框
     */  (function() {
        //创建配置对象
        var _cfg = {
          "mdlhome" : document.documentElement.lastChild.lastChild.src.replace(new RegExp("(.*\/){0,}([^\.]+.*)", "ig"), "$1"),
            "shade" : null,
            "alone" : null,
            "combo" : null
        };    //创建盒子对象
        var _bin = function(that, model) {
          try {
            return function(p) {          //创建内置常量
              var cfg = shouyetong.cfg["plugins_dialog"];          //创建内置方法
              var fun = {
                createShade : function() {
                  var d = cfg["shade"];
                  if(!d) {
                    d = that.createElement("DIV", {
                      style : (function() {
                        return "position:fixed; top:0px; left:0px; z-index:1000000000; overflow:hidden; width:100%; height:100%; margin:0px; border:none; padding:0px; background:#000000;" +
                                "opacity:.50; -moz-opacity:.50; filter:alpha(opacity=50);" +
                                "_position:absolute; _bottom:auto; _top:expression(eval(document.documentElement.scrollTop));";
                      })(),
                      onclick : function(event) {
                        that.cancelBubble(event || p.window.event);
                      },
                      ondblclick : function(event) {
                        that.cancelBubble(event || p.window.event);
                      },
                      onmousedown : function(event) {
                        that.cancelBubble(event || p.window.event);
                      },
                      onmouseup : function(event) {
                        that.cancelBubble(event || p.window.event);
                      }
                    }, null, p.window);
                    cfg["shade"] = d;
                    p.window.document.body.appendChild(d);
                  }
                },
                removeShade : function() {
                  var d = cfg["shade"];
                  if(d) {
                    cfg["shade"] = null;
                    p.window.document.body.removeChild(d);
                  }
                },
                configMdlly : function() {
                  var d = p.alone ? cfg["alone"] : cfg["combo"][p.index];
                  var m = (function() {
                    if(/<.*>/.test(model)) {
                      return model;
                    } else {
                      var f = that.createElement("IFRAME", {src : model.indexOf("@") < 0 ? cfg["mdlhome"] + model + "/model.htm" : "/" + model.substring(1) + "/model.htm", style : "display:none;"}, null, p.window);
                      that.attachEvent(f, {
                        load : function() {
                          m = f.contentWindow.document.body.innerHTML;
                          d.removeChild(f);
                        }
                      });
                      d.appendChild(f);
                    }
                  })();
                  var t1 = setInterval(function() {
                    if(m) {
                      window.clearInterval(t1);
                      for(var a in p) {
                        m = m.replace(new RegExp("{=" + a + "}|%7B=" + a + "%7D", "ig"), p[a]);
                      }
                      if(p.script) {
                        m = m.replace(new RegExp("<!--(<script.*>[.\n\r]*<\/script>)-->", "ig"), "$1");
                      }
                      d.innerHTML = m;
                      that.ergodicElement(d, function(e) {
                        e.action = function() {act(arguments)};
                        e.example = that;
                      });
                    }
                  }, 100);
                  var t2 = setInterval(function() {
                    if(d.innerHTML) {
                      window.clearInterval(t2);
                      var _a = that.getDocumentByScroll(p.window).x;
                      var _b = that.getDocumentByScroll(p.window).y;
                      var _c = that.getDocumentBySize(p.window).width;
                      var _d = that.getDocumentBySize(p.window).height;
                      var _w = d.offsetWidth;
                      var _h = d.offsetHeight;
                      d.style.left = (_a + (_c - _w) / 2) + "px";
                      d.style.top = (_b + (_d - _h) / 2) + "px";
                      d.focus();
                      that.createAnimate(d, 10, 10, {
                        "opacity" : function(f, d) {
                          return f * 10 / 100;
                        },
                        "-moz-opacity" : function(f, d) {
                          return f * 10 / 100;
                        },
                        "filter" : function(f, d) {
                          return "alpha(opacity=" + f * 10 + ")";
                        }
                      });
                    }
                  }, 100);
                },
                createMdlly : function() {
                  var d = that.createElement("DIV", {
                    style : (function() {
                      return "position:absolute; z-index:2000000000; overflow:hidden; width:auto; height:auto; margin:0px; border:none; padding:0px; background:none;" +
                              "opacity:.0; -moz-opacity:.0; filter:alpha(opacity=0);";
                    })(),
                    tabindex : (function() {
                      return "0";
                    })(),
                    onfocus : function(event) {
                      p.shade ? fun.createShade() : fun.removeShade();
                      p.append ? p.window.document.body.appendChild(d) : null;
                    },
                    onclick : function(event) {
                      that.cancelBubble(event || p.window.event);
                    },
                    ondblclick : function(event) {
                      that.cancelBubble(event || p.window.event);
                    },
                    onmousedown : function(event) {
                      that.cancelBubble(event || p.window.event);
                    },
                    onmouseup : function(event) {
                      that.cancelBubble(event || p.window.event);
                    }
                  }, null, p.window);
                  p.alone ? cfg["alone"] = d : (cfg["combo"] = cfg["combo"] || {}, cfg["combo"][p.index] = d);
                  p.window.document.body.appendChild(d);
                },
                removeMdlly : function() {
                  var d = cfg["alone"];
                  if(d) {
                    cfg["alone"] = null;
                    p.window.document.body.removeChild(d);
                  }
                  if(p.alone) {
                    d = cfg["combo"];
                    if(d) {
                      for(var i in d) {p.window.document.body.removeChild(d[i])}
                      cfg["combo"] = null;
                    }
                  }
                }
              };          //创建内置行为
              var act = function(a) {
                var d = p.alone ? cfg["alone"] : cfg["combo"][p.index];            /******************** FUNCTION ********************/
                if(a[0] == "drag") {
                  that.action("drag", {
                    window : p.window,
                     event : a[1],
                    target : d
                  });
                }
                /******************** FUNCTION ********************/
                /******************** FUNCTION ********************/
                if(a[0] == "close") {
                  p.window.document.body.removeChild(d);
                  if(!p.alone) {
                    delete cfg["combo"][p.index];
                    for(var i in cfg["combo"]) {
                      d = cfg["combo"][i];
                      break;
                    }
                  } else {
                    cfg["alone"] = null;
                  }
                  if(!i) {
                    fun.removeShade();
                    return;
                  }
                }
                /******************** FUNCTION ********************/            d.focus();
              };          //初始化
              p.window = p.window || top;
              p.index = p.index || (Math.random() * 1E18).toString(16).toUpperCase();
              p.ibase = p.ibase || model.indexOf("@") < 0 ? cfg["mdlhome"] + model + "/" : "/" + model.substring(1) + "/";
              p.shade = p.shade !== undefined ? p.shade : true;
              p.alone = p.alone !== undefined ? p.alone : true;
              p.script = p.script !== undefined ? p.script : true;
              p.append = p.append !== undefined ? p.append : true;
              fun.removeMdlly();
              fun.createMdlly();
              fun.configMdlly();
              return function() {act(arguments)};        };
          } catch(error) {      }
        };    //注册组件
        shouyetong.cfg["plugins_dialog"] = _cfg;
        shouyetong.bin.plugins["dialog"] = _bin;
        shouyetong.bin.plugins["对话框"] = _bin;
      })();
      

  3.   

    就要要把innerHTML中的这样的内容:<!--<script type"text/javascript">
    w</script>-->
    替换成:<script type"text/javascript">
    w</script>
      

  4.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head> 
    </head> 
     <body> 
    <script type="text/javascript">
        var html = '<!--<script type\"text/javascript\">  \n asdsd \n <\/script>-->';
        var reg = /<!--(<script[^>]*>[\s\S]*<\/script>)-->/gi;
        alert(html);
        alert(html.replace(reg,"$1"));
    </script>
    </body>
    </html>
      

  5.   

    /<!--(<script[^>]*>[\s\S]*<\/script>)-->/gi 写成:new RegExp("<!--(<script[^>]*>[\s\S]*<\/script>)-->", "gi")为什么就不好使了?
      

  6.   


    写在RegExp里要加\\new RegExp("<!--(<script[^>]*>[\\s\\S]*<\\/script>)-->", "gi")
      

  7.   

    我知道,JS先把“\”当字符串的转义符处理了,处理后的字符串就应该是这样的:<!--(<script[^>]*>[sS]*</script>)-->这样一个正则怎么能正常解析,而且正则也是用“\”做转义符的,所以用new RegExp创建正则要打2次“\”。正则直接量不用的原因,是因为它不是一个字符串,所以“\”也就不会被当作字符串的转义符多转义一次。
      

  8.   

    我竟犯这种低级错,上次也是用字符串.replace,结果替换完的字符串没有任何变化,我就纳闷了半天,结果发现没有个字符串赋值。应该这么写:字符串 = 字符串.replace我给写成了这样:字符串.replace还纳闷了半天……
      

  9.   

    匹配任意字符时,用
    (.|\n)
    因为小数点不能匹配换行符,所以要取一个并集,而
    [.\r\n]
    中的小数点在字符组[]中,所以被转义了,匹配的就是小数点本身,所以[.\r\n]匹配的就是“.”、“\r”、“\n”这三个字符中的一个,达不到匹配任意字符的目的(.|\n)的效率不如字符组
    [\s\S]、[\w\W]、[\d\D]
    其中[\d\D]的性能高些,而[\s\S]的用法被用习惯了,所以更常见一些另外
    var reg = /<!--(<script[^>]*>[\s\S]*<\/script>)-->/gi;
    这里要写作
    var reg = /<!--(<script[^>]*>[\s\S]*?<\/script>)-->/gi;
    因为使用了/g模式,说明可能会出现多个匹配项需要进行替换,使用贪婪模式时,两个匹配项中间的内容整体会被替换,类似于以下结构
    <!--<script type"text/javascript">
    匹配项一
    </script>-->
    中间内容
    <!--<script type"text/javascript">
    匹配项二
    </script>-->替换为$1的结果是<script type"text/javascript">
    匹配项一
    </script>-->
    中间内容
    <!--<script type"text/javascript">
    匹配项二
    </script>
      

  10.   

    var scr = '<!--<script type"text/javascript">\nasdsd</script>-->';
    scr = scr.replace(/<!--(<script.*>[\s\S]*?<\/script>)-->/gi, "$1");
    alert(scr);