1
- # Alternation (OR) |
1
+ # 論理和指定子( Alternation) (OR) |
2
2
3
- Alternation is the term in regular expression that is actually a simple "OR".
3
+ 論理和指定子は、実際には単純な "OR" である正規表現の用語です。
4
4
5
- In a regular expression it is denoted with a vertical line character ` pattern:| ` .
5
+ 正規表現では、縦線の文字 ` pattern:| ` で表現されます。
6
6
7
7
[ cut]
8
8
9
- For instance, we need to find programming languages : HTML, PHP, Java or JavaScript.
9
+ 例えば、プログラム言語を探す必要があるとします : HTML, PHP, Java, または JavaScript です。
10
10
11
- The corresponding regexp : ` pattern:html|php|java(script)? ` .
11
+ 対応する正規表現は次の通りです : ` pattern:html|php|java(script)? ` .
12
12
13
- A usage example :
13
+ 利用例 :
14
14
15
15
``` js run
16
16
let reg = / html| php| css| java(script)? / gi ;
@@ -20,50 +20,50 @@ let str = "First HTML appeared, then CSS, then JavaScript";
20
20
alert ( str .match (reg) ); // 'HTML', 'CSS', 'JavaScript'
21
21
```
22
22
23
- We already know a similar thing -- square brackets. They allow to choose between multiple character, for instance ` pattern:gr[ae]y ` matches ` match:gray ` or ` match:grey ` .
23
+ 私たちは既に同様のことを知っています -- 角括弧です。 ` pattern:gr[ae]y ` は ` match:gray ` または ` match:grey ` にマッチするように、複数の文字から選択することができます。
24
24
25
- Alternation works not on a character level, but on expression level. A regexp ` pattern:A|B|C ` means one of expressions ` A ` , ` B ` or ` C ` .
25
+ 論理和指定子は文字レベルで動作するのではなく、式のレベルで動作します。正規表現 ` pattern:A|B|C ` は ` A ` , ` B ` または ` C ` の式のいずれか、を意味します。
26
26
27
- For instance :
27
+ 例 :
28
28
29
- - ` pattern:gr(a|e)y ` means exactly the same as ` pattern:gr[ae]y ` .
30
- - ` pattern:gra|ey ` means "gra" or "ey".
29
+ - ` pattern:gr(a|e)y ` はまさに ` pattern:gr[ae]y ` と同じ意味です。
30
+ - ` pattern:gra|ey ` は "gra" または "ey" を意味します。
31
31
32
- To separate a part of the pattern for alternation we usually enclose it in parentheses, like this : ` pattern:before(XXX|YYY)after ` .
32
+ 論理和指定子では、パターンの一部を区切るには通常次のようにカッコで囲みます。 : ` pattern:before(XXX|YYY)after ` .
33
33
34
- ## Regexp for time
34
+ ## 時間の正規表現
35
35
36
- In previous chapters there was a task to build a regexp for searching time in the form ` hh:mm ` , for instance ` 12:00 ` . But a simple ` pattern:\d\d:\d\d ` is too vague. It accepts ` 25:99 ` as the time.
36
+ 前のチャプターで、 ` 12:00 ` のような形式 ` hh:mm ` の時間を探す正規表現を構築するタスクがありました。しかし、単純な ` pattern:\d\d:\d\d ` はあまりにも要領を得ません。これは ` 25:99 ` も時間として許容します。
37
37
38
- How can we make a better one?
38
+ どうすればより優れた正規表現を作れるでしょうか?
39
39
40
- We can apply more careful matching :
40
+ その方法として、私たちはより慎重なマッチングを適用することができます :
41
41
42
- - The first digit must be ` 0 ` or ` 1 ` followed by any digit.
43
- - Or ` 2 ` followed by ` pattern:[0-3] `
42
+ - 最初の数字は ` 0 ` か ` 1 ` で、その後に任意の数値が続く必要があります。
43
+ - もしくは ` 2 ` でその後に ` pattern:[0-3] ` が続きます。
44
44
45
- As a regexp: ` pattern:[01]\d|2[0-3] ` .
45
+ 正規表現としてはこのようになります: ` pattern:[01]\d|2[0-3] ` .
46
46
47
- Then we can add a colon and the minutes part.
47
+ その後、コロンと分の部分を追加します。
48
48
49
- The minutes must be from ` 0 ` to ` 59 ` , in the regexp language that means the first digit ` pattern:[0-5] ` followed by any other digit ` \d ` .
49
+ 分 は ` 0 ` から ` 59 ` までである必要があり、正規表現では最初の数字 ` pattern:[0-5] ` でその後に他の数字 ` \d ` が続くことを意味します。
50
50
51
- Let's glue them together into the pattern: ` pattern:[01]\d|2[0-3]:[0-5]\d ` .
52
-
53
- We're almost done, but there's a problem. The alternation ` | ` is between the ` pattern:[01]\d ` and ` pattern:2[0-3]:[0-5]\d ` . That's wrong, because it will match either the left or the right pattern:
51
+ パターンにそれらを引っ付けましょう: ` pattern:[01]\d|2[0-3]:[0-5]\d ` .
54
52
53
+ ほとんど完了していますが、まだ問題があります。論理和指定子 ` | ` は ` pattern:[01]\d ` と ` pattern:2[0-3]:[0-5]\d ` の間です。これだと左右どちらかのパターンがマッチするすることになるので間違いです。
55
54
56
55
``` js run
57
56
let reg = / [01] \d | 2[0-3 ] :[0-5 ] \d / g ;
58
57
59
- alert (" 12" .match (reg)); // 12 (matched [01]\d)
58
+ alert (" 12" .match (reg)); // 12 ([01]\d にマッチ )
60
59
```
61
60
62
- That's rather obvious, but still an often mistake when starting to work with regular expressions.
61
+ それはかなり明らかではありますが、正規表現で作業を開始するときには依然としてよく起きるミスです。
62
+
63
+ 時間の部分 ` [01]\d ` OR ` 2[0-3] ` へ正確に論理和指定子を適用するために括弧が必要です。
63
64
64
- We need to add parentheses to apply alternation exactly to hours: ` [01]\d ` OR ` 2[0-3] ` .
65
65
66
- The correct variant :
66
+ 正しいバリアントです :
67
67
68
68
``` js run
69
69
let reg = / ([01] \d | 2[0-3 ] ):[0-5 ] \d / g ;
0 commit comments