简体   繁体   English

Firefox扩展中的Javascript正则表达式

[英]Javascript regex in firefox extension

What could be going wrong here? 这里可能出什么问题了? The problem seems to be with the regex match statement. 问题似乎出在正则表达式匹配语句上。 without it the alerts come, but as soon as I put it in everything goes quiet. 没有它,警报就会来,但是一旦我将其放入,一切都会变得安静。 Thanks so much! 非常感谢! the wildcard is simply to help pinpoint the problem, it is NOT the objective, i do need regex. 通配符只是为了帮助查明问题,这不是目标,我确实需要正则表达式。

window.addEventListener("load", function() { myExtension.init(); }, false);

var myExtension = {
  init: function() {
    var appcontent = document.getElementById("appcontent");   // browser
    if(appcontent)
      appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);
    var messagepane = document.getElementById("messagepane"); // mail
    if(messagepane)
      messagepane.addEventListener("load", function () { myExtension.onPageLoad(); }, true);
  },

  onPageLoad: function(aEvent) {
    var doc = aEvent.originalTarget; // doc is document that triggered "onload" event
    // do something with the loaded page.
    // doc.location is a Location object (see below for a link).
    // You can use it to make your code executed on certain pages only.

    var url = doc.location;  // i have also tried doc.location.href

    if (url.match(.*)) {alert( url );}

  }
}

match takes a regexp object. match需要一个正则表达式对象。 A regexp object can be constructed with a literal of form /regex here/ (note the slashes). 可以使用/regex here/形式的文字构造regexp对象(请注意斜杠)。

With your code you should see a syntax error reported to the Error Console . 使用您的代码,您应该看到向错误控制台报告的语法错误。

[edit] to be 100% clear, you need .match(/.*/) instead of .match(.*) . [edit]要100%清除,您需要.match(/.*/)而不是.match(.*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM