For the sake of understanding MatchPattern
, I am trying this SDK code (because it does not work in webextensions). I need to write a pattern for matching any URL of the form:
http://[anything.]myArray[index][/anything]
Where the [anything.]
and [/anything]
are optional. The code gives errors and does not work. For example, this an error that I’m getting:
The match result of this is code is always false (no match is found). Can you clarify what I am missing? Is the RegEx written in a wrong format?
This is the code I am running:
index.js
:
var { MatchPattern } = require("sdk/util/match-pattern");
var { Cc,Ci,Cu,CC } = require("chrome");
Cu.import("resource://gre/modules/MatchPattern.jsm");
Cu.import("resource://gre/modules/BrowserUtils.jsm");
var myArray = ["facebook.com", "google.com"];
var link="http://www.google.com/xys";
for (var x=0; x<myArray.length; x++)
{
var match = new MatchPattern("(http:\\/\\/)(.*\\.)*"+"("+myArray[x]+")"+"(\\/.*)*(\\/)*");
console.log("loop: "+x);
var uri=BrowserUtils.makeURI(link);
console.log("match result is: "+match.matches(uri));
}//end for loop
and the package.json
:
{
"title": "My Jetpack Addon",
"name": "test",
"version": "0.0.1",
"description": "A basic add-on",
"main": "index.js",
"engines": {
"firefox": ">=38.0a1",
"fennec": ">=38.0a1"
},
"license": "MIT",
"keywords": [
"jetpack"
]
}