aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.5/packages/api-utils/docs/match-pattern.md
blob: 0222e655668ffbc50e86a2555905c14f8cf6f138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

The `match-pattern` module can be used to test strings containing URLs
against simple patterns.

## Specifying Patterns ##

There are three ways you can specify patterns:

* as an exact match string
* using a wildcard in a string
* using a regular expression

### Exact Matches ###

**A URL** matches only that URL. The URL must start with a scheme, end with a
slash, and contain no wildcards.

<table>

  <colgroup>
    <col width="30%">
    <col width="35%">
    <col width="35%">
  </colgroup>

  <tr>
    <th>Example pattern</th>
    <th>Example matching URLs</th>
    <th>Example non-matching URLs</th>
  </tr>

  <tr>
    <td><code>"http://example.com/"</code></td>
    <td><code>http://example.com/</code></td>
    <td><code>http://example.com</code><br>
        <code>http://example.com/foo</code><br>
        <code>https://example.com/</code><br>
        <code>http://foo.example.com/</code></td>
  </tr>

</table>

### Wildcards ###

**A single asterisk** matches any URL with an `http`, `https`, or `ftp`
scheme. For other schemes like `file`, use a scheme followed by an
asterisk, as below.

<table>

  <colgroup>
    <col width="30%">
    <col width="35%">
    <col width="35%">
  </colgroup>

  <tr>
    <th>Example pattern</th>
    <th>Example matching URLs</th>
    <th>Example non-matching URLs</th>
  </tr>

  <tr>
    <td><code>"*"</code></td>
    <td><code>http://example.com/</code><br>
        <code>https://example.com/</code><br>
        <code>ftp://example.com/</code><br>
        <code>http://bar.com/foo.js</code><br>
        <code>http://foo.com/</code></td>
    <td><code>file://example.js</code></td>
  </tr>

</table>

**A domain name prefixed with an asterisk and dot** matches any URL of that
domain or a subdomain, using any of `http`, `https`, `ftp`.

<table>

  <colgroup>
    <col width="30%">
    <col width="35%">
    <col width="35%">
  </colgroup>

  <tr>
    <th>Example pattern</th>
    <th>Example matching URLs</th>
    <th>Example non-matching URLs</th>
  </tr>

  <tr>
    <td><code>"*.example.com"</code></td>
    <td><code>http://example.com/</code><br>
        <code>http://foo.example.com/</code><br>
        <code>https://example.com/</code><br>
        <code>http://example.com/foo</code><br>
        <code>ftp://foo.example.com/</code></td>
    <td><code>ldap://example.com</code><br>
        <code>http://example.foo.com/</code></td>
  </tr>

</table>

**A URL followed by an asterisk** matches that URL and any URL prefixed with
the pattern.

<table>

  <colgroup>
    <col width="30%">
    <col width="35%">
    <col width="35%">
  </colgroup>

  <tr>
    <th>Example pattern</th>
    <th>Example matching URLs</th>
    <th>Example non-matching URLs</th>
  </tr>

  <tr>
    <td><code>"https://foo.com/*"</code></td>
    <td><code>https://foo.com/</code><br>
        <code>https://foo.com/bar</code></td>
    <td><code>http://foo.com/</code><br>
        <code>https://foo.com</code><br>
        <code>https://bar.foo.com/</code></td>
  </tr>

</table>

**A scheme followed by an asterisk** matches all URLs with that scheme. To
match local files, use `file://*`.

<table>

  <colgroup>
    <col width="30%">
    <col width="70%">
  </colgroup>

  <tr>
    <th>Example pattern</th>
    <th>Example matching URLs</th>
  </tr>

  <tr>
    <td><code>"file://*"</code></td>
    <td><code>file://C:/file.html</code><br>
        <code>file:///home/file.png</code></td>
  </tr>

</table>

### Regular Expressions ###

You can specify patterns using a
[regular expression](https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions):

    var { MatchPattern } = require("match-pattern");
    var pattern = new MatchPattern(/.*example.*/);

The regular expression is subject to restrictions based on those applied to the
[HTML5 pattern attribute](http://dev.w3.org/html5/spec/common-input-element-attributes.html#attr-input-pattern). In particular:

* The pattern must match the entire value, not just any subset. For example, the
pattern `/moz.*/` will not match the URL `http://mozilla.org`.

* The expression is compiled with the `global`, `ignoreCase`, and `multiline` flags
  disabled. The `MatchPattern` constructor will throw an exception
  if you try to set any of these flags.

<table>

  <colgroup>
    <col width="30%">
    <col width="35%">
    <col width="35%">
  </colgroup>

  <tr>
    <th>Example pattern</th>
    <th>Example matching URLs</th>
    <th>Example non-matching URLs</th>
  </tr>

  <tr>
    <td><code>/.*moz.*/</code></td>
    <td><code>http://foo.mozilla.org/</code><br>
        <code>http://mozilla.org</code><br>
        <code>https://mozilla.org</code><br>
        <code>http://foo.com/mozilla</code><br>
        <code>http://hemozoon.org</code><br>
        <code>mozscheme://foo.org</code><br></td>
    <td><code>http://foo.org</code><br>
  </tr>

  <tr>
    <td><code>/http:\/\/moz.*/</code></td>
    <td><code>http://mozilla.org</code><br>
        <code>http://mozzarella.com</code></td>
    <td><code>https://mozilla.org</code><br>
        <code>http://foo.mozilla.org/</code><br>
        <code>http://foo.com/moz</code></td>
  </tr>

  <tr>
    <td><code>/http.*moz.*/</code><br></td>
    <td><code>http://foo.mozilla.org/</code><br>
        <code>http://mozilla.org</code><br>
        <code>http://hemozoon.org/</code></td>
        <td><code>ftp://http/mozilla.org</code></td>
  </tr>

</table>

## Examples ##

    var { MatchPattern } = require("match-pattern");
    var pattern = new MatchPattern("http://example.com/*");
    console.log(pattern.test("http://example.com/"));       // true
    console.log(pattern.test("http://example.com/foo"));    // true
    console.log(pattern.test("http://foo.com/"));           // false!

<api name="MatchPattern">
@class
<api name="MatchPattern">
@constructor
  This constructor creates match pattern objects that can be used to test URLs.
@param pattern {string}
  The pattern to use.  See Patterns above.
</api>

<api name="test">
@method
  Tests a URL against the match pattern.
@param url {string}
  The URL to test.
@returns {boolean}
  True if the URL matches the pattern and false otherwise.
</api>
</api>