|
1 |
| -# Patterns and flags |
| 1 | +# パターンとフラグ |
2 | 2 |
|
3 |
| -Regular expressions is a powerful way of searching and replacing inside a string. |
| 3 | +正規表現(Regular expressions)は文字列内を検索したり置換するための強力な方法です。 |
4 | 4 |
|
5 |
| -In JavaScript regular expressions are implemented using objects of a built-in `RegExp` class and integrated with strings. |
| 5 | +JavaScriptでは、正規表現は組み込みの `RegExp` クラスのオブジェクトを使用して実装され、文字列と統合されています。 |
6 | 6 |
|
7 |
| -Please note that regular expressions vary between programming languages. In this tutorial we concentrate on JavaScript. Of course there's a lot in common, but they are a somewhat different in Perl, Ruby, PHP etc. |
| 7 | +正規表現はプログラミング言語によって異なることに留意してください。このチュートリアルでは、JavaScript に焦点を当てます。もちろん共通点は多いですが、Perl, Ruby, PHP などとは多少異なります。 |
8 | 8 |
|
9 | 9 | [cut]
|
10 | 10 |
|
11 |
| -## Regular expressions |
| 11 | +## 正規表現 |
12 | 12 |
|
13 |
| -A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*. |
| 13 | +正規表現(もしくは "regexp", または単に "reg") は *パターン* とオプションの *フラグ* で構成されています。 |
14 | 14 |
|
15 |
| -There are two syntaxes to create a regular expression object. |
| 15 | +正規表現オブジェクトを生成するための2つの構文があります。 |
16 | 16 |
|
17 |
| -The long syntax: |
| 17 | +長い構文: |
18 | 18 |
|
19 | 19 | ```js
|
20 | 20 | regexp = new RegExp("pattern", "flags");
|
21 | 21 | ```
|
22 | 22 |
|
23 |
| -...And the short one, using slashes `"/"`: |
| 23 | +...そして短い構文です。スラッシュ `"/"` を使います: |
24 | 24 |
|
25 | 25 | ```js
|
26 |
| -regexp = /pattern/; // no flags |
27 |
| -regexp = /pattern/gmi; // with flags g,m and i (to be covered soon) |
| 26 | +regexp = /pattern/; // フラグなし |
| 27 | +regexp = /pattern/gmi; // g, m と i のフラグあり(詳細は後ほど説明します) |
28 | 28 | ```
|
29 | 29 |
|
30 |
| -Slashes `"/"` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings. |
| 30 | +スラッシュ `"/"` は正規表現を作成していることを JavaScript に伝えます。文字列の引用符と同じ役割を果たします。 |
31 | 31 |
|
32 |
| -## Usage |
| 32 | +## 使用方法 |
33 | 33 |
|
34 |
| -To search inside a string, we can use method [search](mdn:js/String/search). |
| 34 | +文字列内を検索するためには、メソッド [search](mdn:js/String/search) を使うことができます。 |
35 | 35 |
|
36 |
| -Here's an example: |
| 36 | +例: |
37 | 37 |
|
38 | 38 | ```js run
|
39 |
| -let str = "I love JavaScript!"; // will search here |
| 39 | +let str = "I love JavaScript!"; // ここを検索します |
40 | 40 |
|
41 | 41 | let regexp = /love/;
|
42 | 42 | alert( str.search(regexp) ); // 2
|
43 | 43 | ```
|
44 | 44 |
|
45 |
| -The `str.search` method looks for the pattern `pattern:/love/` and returns the position inside the string. As we might guess, `pattern:/love/` is the simplest possible pattern. What it does is a simple substring search. |
| 45 | +`str.search` メソッドはパターン `pattern:/love/` を探し、文字列内での位置を返します。ご推測の通り、 `pattern:/love/` は最もシンプルなパターンです。それは簡単な部分文字列検索です。 |
46 | 46 |
|
47 |
| -The code above is the same as: |
| 47 | +上のコードは次と同じです: |
48 | 48 |
|
49 | 49 | ```js run
|
50 |
| -let str = "I love JavaScript!"; // will search here |
| 50 | +let str = "I love JavaScript!"; // ここを検索します |
51 | 51 |
|
52 | 52 | let substr = 'love';
|
53 | 53 | alert( str.search(substr) ); // 2
|
54 | 54 | ```
|
55 | 55 |
|
56 |
| -So searching for `pattern:/love/` is the same as searching for `"love"`. |
| 56 | +したがって、`pattern:/love/` の検索は `"love"` の検索と同じです。 |
57 | 57 |
|
58 |
| -But that's only for now. Soon we'll create more complex regular expressions with much searching more power. |
| 58 | +しかし、それは今だけです。すぐにより強力な検索機能を備えた、より複雑な正規表現作成していきます。 |
59 | 59 |
|
60 |
| -```smart header="Colors" |
61 |
| -From here on the color scheme is: |
| 60 | +```smart header="色" |
| 61 | +ここからの配色は次の通りです: |
62 | 62 |
|
63 |
| -- regexp -- `pattern:red` |
64 |
| -- string (where we search) -- `subject:blue` |
65 |
| -- result -- `match:green` |
| 63 | +- 正規表現 -- `pattern:red` |
| 64 | +- 文字列 (検索する場所) -- `subject:blue` |
| 65 | +- 結果 -- `match:green` |
66 | 66 | ```
|
67 | 67 |
|
68 | 68 |
|
69 |
| -````smart header="When to use `new RegExp`?" |
70 |
| -Normally we use the short syntax `/.../`. But it does not allow any variables insertions, so we must know the exact regexp at the time of writing the code. |
| 69 | +````smart header="いつ `new RegExp` を使いますか?" |
| 70 | +通常は短い構文である `/.../` を使います。しかし、これは変数の挿入を許可していないため、コードを書く時点で正確な正規表現を知っていなければなりません。 |
71 | 71 |
|
72 |
| -From the other hand, `new RegExp` allows to construct a pattern dynamically from a string. |
| 72 | +一方、`new RegExp` は文字列から動的にパターンを構築することができます。 |
73 | 73 |
|
74 |
| -So we can figure out what we need to search and create `new RegExp` from it: |
| 74 | +したがって、検索するために必要なことを理解し、そこから `new RegExp` を作ることができます。: |
75 | 75 |
|
76 | 76 | ```js run
|
77 | 77 | let search = prompt("What you want to search?", "love");
|
78 | 78 | let regexp = new RegExp(search);
|
79 | 79 |
|
80 |
| -// find whatever the user wants |
| 80 | +// ユーザが望むものを見つける |
81 | 81 | alert( "I love JavaScript".search(regexp));
|
82 | 82 | ```
|
83 | 83 | ````
|
84 | 84 |
|
85 | 85 |
|
86 |
| -## Flags |
| 86 | +## フラグ |
87 | 87 |
|
88 |
| -Regular expressions may have flags that affect the search. |
| 88 | +正規表現には検索に影響を与えるフラグを含んでいる場合があります。 |
89 | 89 |
|
90 |
| -There are only 5 of them in JavaScript: |
| 90 | +JavaScript には 5 つしかありません: |
91 | 91 |
|
92 | 92 | `i`
|
93 |
| -: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below). |
| 93 | +: このフラグを指定すると、検索は大文字小文字を区別しません: `A` と `a` に違いはありません(下の例をみてください)。 |
94 | 94 |
|
95 | 95 | `g`
|
96 |
| -: With this flag the search looks for all matches, without it -- only the first one (we'll see uses in the next chapter). |
| 96 | +: このフラグを指定すると、検索はすべての一致を探します。指定がない場合は -- 最初の1つのみを探します(次のチャプターで使い方を見ていきます)。 |
97 | 97 |
|
98 | 98 | `m`
|
99 |
| -: Multiline mode (covered in the chapter <info:regexp-multiline>). |
| 99 | +: 複数行モードです(チャプター <info:regexp-multiline> で説明します)。 |
100 | 100 |
|
101 | 101 | `u`
|
102 |
| -: Enables full unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter <info:regexp-unicode>. |
| 102 | +: 完全なユニコードサポートを有効にします。このフラグはサロゲートペアの正しい処理を可能にします。より詳細についてはチャプター <info:regexp-unicode> を参照してください。 |
103 | 103 |
|
104 | 104 | `y`
|
105 |
| -: Sticky mode (covered in the [next chapter](info:regexp-methods#y-flag)) |
| 105 | +: スティッキーモード([次のチャプター](info:regexp-methods#y-flag) で説明します)。 |
106 | 106 |
|
| 107 | +## "i" フラグ |
107 | 108 |
|
108 |
| -## The "i" flag |
| 109 | +最も簡単なフラグは `i` です。 |
109 | 110 |
|
110 |
| -The simplest flag is `i`. |
111 |
| -
|
112 |
| -An example with it: |
| 111 | +その例です: |
113 | 112 |
|
114 | 113 | ```js run
|
115 | 114 | let str = "I love JavaScript!";
|
116 | 115 |
|
117 |
| -alert( str.search(/LOVE/) ); // -1 (not found) |
| 116 | +alert( str.search(/LOVE/) ); // -1 (見つからない) |
118 | 117 | alert( str.search(/LOVE/i) ); // 2
|
119 | 118 | ```
|
120 | 119 |
|
121 |
| -1. The first search returns `-1` (not found), because the search is case-sensitive by default. |
122 |
| -2. With the flag `pattern:/LOVE/i` the search found `match:love` at position 2. |
| 120 | +1. 最初の検索は `-1` (見つからない) を返します。なぜなら、デフォルトでは検索は大文字小文字を区別するためです。 |
| 121 | +2. フラグ `pattern:/LOVE/i` を指定すると、検索は位置 2 に `match:love` を見つけます。 |
123 | 122 |
|
124 |
| -So the `i` flag already makes regular expressions more powerful than a simple substring search. But there's so much more. We'll cover other flags and features in the next chapters. |
| 123 | +したがって、`i` フラグはすでに単純な部分文字列検索よりも強力な正規表現を作成します。しかし、まだまだはるかに多くのことがあります。次のチャプターでは、他のフラグと機能についても説明します。 |
125 | 124 |
|
126 | 125 |
|
127 |
| -## Summary |
| 126 | +## サマリ |
128 | 127 |
|
129 |
| -- A regular expression consists of a pattern and optional flags: `g`, `i`, `m`, `u`, `y`. |
130 |
| -- Without flags and special symbols that we'll study later, the search by a regexp is the same as a substring search. |
131 |
| -- The method `str.search(regexp)` returns the index where the match is found or `-1` if there's no match. |
| 128 | +- 正規表現はパターンとオプションのフラグ `g`, `i`, `m`, `u`, `y` で構成されます。 |
| 129 | +- フラグと後で学ぶ特別な記号がなければ、正規表現による検索は部分文字列検索と同じです。 |
| 130 | +- メソッド `str.search(regexp)` は一致するものが見つかった場所はそのインデックスを返します。見つからなかった場合は `-1` を返します。 |
0 commit comments