Skip to content

Commit f1c98dc

Browse files
committed
Translate 5-regular-expressions/01-regexp-introduction/article.md
1 parent f908ca4 commit f1c98dc

File tree

1 file changed

+50
-51
lines changed
  • 5-regular-expressions/01-regexp-introduction

1 file changed

+50
-51
lines changed
Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,130 @@
1-
# Patterns and flags
1+
# パターンとフラグ
22

3-
Regular expressions is a powerful way of searching and replacing inside a string.
3+
正規表現(Regular expressions)は文字列内を検索したり置換するための強力な方法です。
44

5-
In JavaScript regular expressions are implemented using objects of a built-in `RegExp` class and integrated with strings.
5+
JavaScriptでは、正規表現は組み込みの `RegExp` クラスのオブジェクトを使用して実装され、文字列と統合されています。
66

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 などとは多少異なります。
88

99
[cut]
1010

11-
## Regular expressions
11+
## 正規表現
1212

13-
A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*.
13+
正規表現(もしくは "regexp", または単に "reg") *パターン* とオプションの *フラグ* で構成されています。
1414

15-
There are two syntaxes to create a regular expression object.
15+
正規表現オブジェクトを生成するための2つの構文があります。
1616

17-
The long syntax:
17+
長い構文:
1818

1919
```js
2020
regexp = new RegExp("pattern", "flags");
2121
```
2222

23-
...And the short one, using slashes `"/"`:
23+
...そして短い構文です。スラッシュ `"/"` を使います:
2424

2525
```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 のフラグあり(詳細は後ほど説明します)
2828
```
2929

30-
Slashes `"/"` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings.
30+
スラッシュ `"/"` は正規表現を作成していることを JavaScript に伝えます。文字列の引用符と同じ役割を果たします。
3131

32-
## Usage
32+
## 使用方法
3333

34-
To search inside a string, we can use method [search](mdn:js/String/search).
34+
文字列内を検索するためには、メソッド [search](mdn:js/String/search) を使うことができます。
3535

36-
Here's an example:
36+
:
3737

3838
```js run
39-
let str = "I love JavaScript!"; // will search here
39+
let str = "I love JavaScript!"; // ここを検索します
4040

4141
let regexp = /love/;
4242
alert( str.search(regexp) ); // 2
4343
```
4444

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/` は最もシンプルなパターンです。それは簡単な部分文字列検索です。
4646

47-
The code above is the same as:
47+
上のコードは次と同じです:
4848

4949
```js run
50-
let str = "I love JavaScript!"; // will search here
50+
let str = "I love JavaScript!"; // ここを検索します
5151

5252
let substr = 'love';
5353
alert( str.search(substr) ); // 2
5454
```
5555

56-
So searching for `pattern:/love/` is the same as searching for `"love"`.
56+
したがって、`pattern:/love/` の検索は `"love"` の検索と同じです。
5757

58-
But that's only for now. Soon we'll create more complex regular expressions with much searching more power.
58+
しかし、それは今だけです。すぐにより強力な検索機能を備えた、より複雑な正規表現作成していきます。
5959

60-
```smart header="Colors"
61-
From here on the color scheme is:
60+
```smart header=""
61+
ここからの配色は次の通りです:
6262
63-
- regexp -- `pattern:red`
64-
- string (where we search) -- `subject:blue`
65-
- result -- `match:green`
63+
- 正規表現 -- `pattern:red`
64+
- 文字列 (検索する場所) -- `subject:blue`
65+
- 結果 -- `match:green`
6666
```
6767

6868

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+
通常は短い構文である `/.../` を使います。しかし、これは変数の挿入を許可していないため、コードを書く時点で正確な正規表現を知っていなければなりません。
7171

72-
From the other hand, `new RegExp` allows to construct a pattern dynamically from a string.
72+
一方、`new RegExp` は文字列から動的にパターンを構築することができます。
7373

74-
So we can figure out what we need to search and create `new RegExp` from it:
74+
したがって、検索するために必要なことを理解し、そこから `new RegExp` を作ることができます。:
7575

7676
```js run
7777
let search = prompt("What you want to search?", "love");
7878
let regexp = new RegExp(search);
7979

80-
// find whatever the user wants
80+
// ユーザが望むものを見つける
8181
alert( "I love JavaScript".search(regexp));
8282
```
8383
````
8484
8585
86-
## Flags
86+
## フラグ
8787
88-
Regular expressions may have flags that affect the search.
88+
正規表現には検索に影響を与えるフラグを含んでいる場合があります。
8989
90-
There are only 5 of them in JavaScript:
90+
JavaScript には 5 つしかありません:
9191
9292
`i`
93-
: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below).
93+
: このフラグを指定すると、検索は大文字小文字を区別しません: `A` `a` に違いはありません(下の例をみてください)。
9494
9595
`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つのみを探します(次のチャプターで使い方を見ていきます)。
9797
9898
`m`
99-
: Multiline mode (covered in the chapter <info:regexp-multiline>).
99+
: 複数行モードです(チャプター <info:regexp-multiline> で説明します)。
100100
101101
`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> を参照してください。
103103
104104
`y`
105-
: Sticky mode (covered in the [next chapter](info:regexp-methods#y-flag))
105+
: スティッキーモード([次のチャプター](info:regexp-methods#y-flag) で説明します)。
106106
107+
## "i" フラグ
107108
108-
## The "i" flag
109+
最も簡単なフラグは `i` です。
109110
110-
The simplest flag is `i`.
111-
112-
An example with it:
111+
その例です:
113112
114113
```js run
115114
let str = "I love JavaScript!";
116115
117-
alert( str.search(/LOVE/) ); // -1 (not found)
116+
alert( str.search(/LOVE/) ); // -1 (見つからない)
118117
alert( str.search(/LOVE/i) ); // 2
119118
```
120119
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` を見つけます。
123122
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` フラグはすでに単純な部分文字列検索よりも強力な正規表現を作成します。しかし、まだまだはるかに多くのことがあります。次のチャプターでは、他のフラグと機能についても説明します。
125124
126125
127-
## Summary
126+
## サマリ
128127
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

Comments
 (0)