Skip to content

Commit 075e078

Browse files
authored
Patterns and flags (#135)
* Patterns and flags * Correction of words, quote style * Change words by suggestions
1 parent eb73376 commit 075e078

File tree

1 file changed

+71
-78
lines changed
  • 9-regular-expressions/01-regexp-introduction

1 file changed

+71
-78
lines changed
Lines changed: 71 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,101 @@
1-
# Patterns and flags
1+
# 樣式(Patterns)與 旗標(flags
22

3-
Regular expressions are patterns that provide a powerful way to search and replace in text.
3+
正規表達式(Regular expressions)是提供一種有力的方式在文字上去搜尋與取代的模式。
44

5-
In JavaScript, they are available via the [RegExp](mdn:js/RegExp) object, as well as being integrated in methods of strings.
5+
JavaScript,它們可以藉由 [RegExp](mdn:js/RegExp) 物件使用,也整合進了字串的方法中。
66

7-
## Regular Expressions
7+
## 正規表達式
88

9-
A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*.
9+
一個正規表達式(又稱 "regexp",或只寫做 "reg")由一個 **樣式** 與可選用的 **旗標** 組成。
1010

11-
There are two syntaxes that can be used to create a regular expression object.
11+
這裡有兩種語法可以用來建立一個正規表達式物件。
1212

13-
The "long" syntax:
13+
較長的語法:
1414

1515
```js
1616
regexp = new RegExp("pattern", "flags");
1717
```
1818

19-
And the "short" one, using slashes `"/"`:
19+
和較短的,使用斜線 `"/"`
2020

2121
```js
22-
regexp = /pattern/; // no flags
23-
regexp = /pattern/gmi; // with flags g,m and i (to be covered soon)
22+
regexp = /pattern/; // 沒有旗標
23+
regexp = /pattern/gmi; // 搭配旗標 g、m 和 i (等一下會講到)
2424
```
2525

26-
Slashes `pattern:/.../` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings.
26+
斜線 `pattern:/.../` 會告訴 JavaScript 我們要建立一個正規表達式。它們扮演著跟字串引號相同的角色。
2727

28-
In both cases `regexp` becomes an instance of the built-in `RegExp` class.
28+
兩種請況下的 `regexp` 都會變成內建 `RegExp` 類別的實體。
2929

30-
The main difference between these two syntaxes is that pattern using slashes `/.../` does not allow for expressions to be inserted (like string template literals with `${...}`). They are fully static.
30+
兩種語法主要不同之處在於一種使用斜線 `/.../`,不允許插入表達式(像是字串樣板字面值 `${...}`)。他們是完全靜態的。
3131

32-
Slashes are used when we know the regular expression at the code writing time -- and that's the most common situation. While `new RegExp`, is more often used when we need to create a regexp "on the fly" from a dynamically generated string. For instance:
32+
斜線用在當我們撰寫程式碼時,所知道的正規表達式 -- 這是最常見的情況。而 `new RegExp`,經常用在我們需要用動態產生的字串建立一個 "即時" 的正規表達式。例如:
3333

3434
```js
3535
let tag = prompt("What tag do you want to find?", "h2");
3636

37-
let regexp = new RegExp(`<${tag}>`); // same as /<h2>/ if answered "h2" in the prompt above
37+
let regexp = new RegExp(`<${tag}>`); // 如果在提示窗中回答 "h2",則等同 /<h2>/
3838
```
3939

40-
## Flags
40+
## 旗標
4141

42-
Regular expressions may have flags that affect the search.
42+
正規表達式可能會有影響搜尋結果的旗標。
4343

44-
There are only 6 of them in JavaScript:
44+
JavaScript 中只有6種:
4545

46-
`pattern:i`
47-
: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below).
46+
`pattern:i`:使用這個旗標會使搜尋不區分大小寫:`A``a` 不分(參見下面的範例)
4847

49-
`pattern:g`
50-
: With this flag the search looks for all matches, without it -- only the first match is returned.
48+
`pattern:g`:使用這個旗標會使搜尋查找所有匹配結果, 沒有這個的話 -- 只有第一個匹配結果會傳回。
5149

52-
`pattern:m`
53-
: Multiline mode (covered in the chapter <info:regexp-multiline-mode>).
50+
`pattern:m`:多行模式 (在此章節 <info:regexp-multiline-mode> 介紹)。
5451

55-
`pattern:s`
56-
: Enables "dotall" mode, that allows a dot `pattern:.` to match newline character `\n` (covered in the chapter <info:regexp-character-classes>).
52+
`pattern:s`:啟用 "dotAll" 模式, 允許句點符號 `pattern:.` 去匹配換行字元 `\n`(在此章節 <info:regexp-character-classes> 介紹)。
5753

58-
`pattern:u`
59-
: Enables full unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter <info:regexp-unicode>.
54+
`pattern:u`:啟用完整 unicode 支援。該旗標啟用了正確的代理對(surrogate pairs)處理。關於更多資訊會列在此章節 <info:regexp-unicode>
6055

61-
`pattern:y`
62-
: "Sticky" mode: searching at the exact position in the text (covered in the chapter <info:regexp-sticky>)
56+
`pattern:y`:"Sticky" 模式:搜尋在文本中的確切位置(在此章節 <info:regexp-sticky> 介紹)
6357

64-
```smart header="Colors"
65-
From here on the color scheme is:
58+
```smart header="顏色"
59+
這裡的配色方式是:
6660
67-
- regexp -- `pattern:red`
68-
- string (where we search) -- `subject:blue`
69-
- result -- `match:green`
61+
- 正規表達式 -- `pattern:紅色`
62+
- 字串(我們搜尋的) -- `subject:藍色`
63+
- 匹配結果 -- `match:綠色`
7064
```
7165

72-
## Searching: str.match
66+
## 搜尋: str.match
7367

74-
As mentioned previously, regular expressions are integrated with string methods.
68+
前面有提到,正規表達式有整合在字串的方法。
7569

76-
The method `str.match(regexp)` finds all matches of `regexp` in the string `str`.
70+
`str.match(regexp)` 方法用 `regexp` 正規表達式尋找在整個 `str` 字串中的所有匹配結果。
7771

78-
It has 3 working modes:
72+
它有 3 種運作模式:
7973

80-
1. If the regular expression has flag `pattern:g`, it returns an array of all matches:
74+
1. 如果正規表達式有旗標 `pattern:g`,此傳回一個全部匹配結果的陣列:
8175
```js run
8276
let str = "We will, we will rock you";
8377

84-
alert( str.match(/we/gi) ); // We,we (an array of 2 substrings that match)
78+
alert( str.match(/we/gi) ); // We,we (一個陣列內含 2 個匹配結果的部份字串)
8579
```
86-
Please note that both `match:We` and `match:we` are found, because flag `pattern:i` makes the regular expression case-insensitive.
80+
請注意 `match:We` `match:we` 皆被找到,是因為旗標 `pattern:i` 使正規表達式不分大小寫。
8781

88-
2. If there's no such flag it returns only the first match in the form of an array, with the full match at index `0` and some additional details in properties:
82+
2. 如果沒有旗標 `pattern:g` 傳回形式會是只有第一個匹配結果的陣列,該陣列中包含一些額外資訊在屬性中,完全匹配結果在索引 `0`
8983
```js run
9084
let str = "We will, we will rock you";
9185
92-
let result = str.match(/we/i); // without flag g
86+
let result = str.match(/we/i); // 不使用旗標 g
9387
94-
alert( result[0] ); // We (1st match)
88+
alert( result[0] ); // We (1st 匹配結果)
9589
alert( result.length ); // 1
9690
97-
// Details:
98-
alert( result.index ); // 0 (position of the match)
99-
alert( result.input ); // We will, we will rock you (source string)
91+
// 額外資訊:
92+
alert( result.index ); // 0 (匹配位置)
93+
alert( result.input ); // We will, we will rock you (來源字串)
10094
```
101-
The array may have other indexes, besides `0` if a part of the regular expression is enclosed in parentheses. We'll cover that in the chapter <info:regexp-groups>.
95+
如果正規表達式的一部分在括號中,則陣列除了 `0` 外還會有其他索引。我們將在此章節介紹 <info:regexp-groups>
10296

103-
3. And, finally, if there are no matches, `null` is returned (doesn't matter if there's flag `pattern:g` or not).
104-
105-
This a very important nuance. If there are no matches, we don't receive an empty array, but instead receive `null`. Forgetting about that may lead to errors, e.g.:
97+
3. 最後,如果沒有匹配結果,會傳回 `null`(與有無 `pattern:g` 旗標無關)。
98+
這是很重要的細節。如果沒有匹配結果,我們不會收到一個空陣列,而是收到 `null`。忘記這件事情會導致錯誤,例如:
10699

107100
```js run
108101
let matches = "JavaScript".match(/HTML/); // = null
@@ -112,50 +105,50 @@ It has 3 working modes:
112105
}
113106
```
114107

115-
If we'd like the result to always be an array, we can write it this way:
108+
如果我們想要結果永遠是一個陣列,我們可以這麼寫:
116109

117110
```js run
118111
let matches = "JavaScript".match(/HTML/)*!* || []*/!*;
119112
120113
if (!matches.length) {
121-
alert("No matches"); // now it works
114+
alert("No matches"); // 現在會動了
122115
}
123116
```
124117

125-
## Replacing: str.replace
118+
## 取代: str.replace
126119

127-
The method `str.replace(regexp, replacement)` replaces matches found using `regexp` in string `str` with `replacement` (all matches if there's flag `pattern:g`, otherwise, only the first one).
120+
`str.replace(regexp, replacement)` 方法用 `regexp` 找到了匹配結果,並在 `str` 字串使用 `replacement` 字串取代。(如果是 `pattern:g` 旗標的話,會是全部匹配結果,反之只會是第一個)。
128121

129-
For instance:
122+
例如:
130123

131124
```js run
132-
// no flag g
125+
// 沒有 g 旗標
133126
alert( "We will, we will".replace(/we/i, "I") ); // I will, we will
134127
135-
// with flag g
128+
// 有 g 旗標
136129
alert( "We will, we will".replace(/we/ig, "I") ); // I will, I will
137130
```
138131

139-
The second argument is the `replacement` string. We can use special character combinations in it to insert fragments of the match:
132+
第二個參數是 `replacement` 字串。我們可以在其中使用特殊字元组来插入匹配結果片段:
140133

141-
| Symbols | Action in the replacement string |
134+
| 符號 | 取代字串的操作 |
142135
|--------|--------|
143-
|`$&`|inserts the whole match|
144-
|<code>$&#096;</code>|inserts a part of the string before the match|
145-
|`$'`|inserts a part of the string after the match|
146-
|`$n`|if `n` is a 1-2 digit number, then it inserts the contents of n-th parentheses, more about it in the chapter <info:regexp-groups>|
147-
|`$<name>`|inserts the contents of the parentheses with the given `name`, more about it in the chapter <info:regexp-groups>|
148-
|`$$`|inserts character `$` |
136+
|`$&`|插入整個匹配結果|
137+
|<code>$&#096;</code>|插入匹配結果前面的字串|
138+
|`$'`|插入匹配結果後面的字串|
139+
|`$n`|如果 `n` 是一個 12 位元的數字,那會插入第 n 個括號中的內容,關於更多在此章節 <info:regexp-groups>|
140+
|`$<name>`|插入給定 `name` 括號中的內容, 關於更多在此章節 <info:regexp-groups>|
141+
|`$$`|插入字元 `$` |
149142

150-
An example with `pattern:$&`:
143+
`pattern:$&` 的範例:
151144

152145
```js run
153146
alert( "I love HTML".replace(/HTML/, "$& and JavaScript") ); // I love HTML and JavaScript
154147
```
155148

156-
## Testing: regexp.test
149+
## 測試: regexp.test
157150

158-
The method `regexp.test(str)` looks for at least one match, if found, returns `true`, otherwise `false`.
151+
`regexp.test(str)` 方法尋找至少一個匹配結果,如果有找到,傳回 `true`,反之則是 `false`
159152

160153
```js run
161154
let str = "I love JavaScript";
@@ -164,14 +157,14 @@ let regexp = /LOVE/i;
164157
alert( regexp.test(str) ); // true
165158
```
166159

167-
Later in this chapter we'll study more regular expressions, walk through more examples, and also meet other methods.
160+
之後的章節我們將學習更多正規表達式,經過更多的範例,了解其他方法。
168161

169-
Full information about the methods is given in the article <info:regexp-methods>.
162+
關於方法的完整資訊在此文章 <info:regexp-methods> 內提供。
170163

171-
## Summary
164+
## 總結
172165

173-
- A regular expression consists of a pattern and optional flags: `pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`.
174-
- Without flags and special symbols (that we'll study later), the search by a regexp is the same as a substring search.
175-
- The method `str.match(regexp)` looks for matches: all of them if there's `pattern:g` flag, otherwise, only the first one.
176-
- The method `str.replace(regexp, replacement)` replaces matches found using `regexp` with `replacement`: all of them if there's `pattern:g` flag, otherwise only the first one.
177-
- The method `regexp.test(str)` returns `true` if there's at least one match, otherwise, it returns `false`.
166+
- 一個正規表達式由一個樣式和可選用的旗標組成,可選用旗標:`pattern:g``pattern:i``pattern:m``pattern:u``pattern:s``pattern:y`
167+
- 除了旗標和特殊符號(我們等等會學到),用正規表達式來搜尋跟部份字串搜尋相同。
168+
- `str.match(regexp)` 方法尋找這些匹配:若有 `pattern:g` 旗標,找到全部匹配,否則,只找第一個匹配。
169+
- `str.replace(regexp, replacement)` 方法用 `regexp` 找到了匹配結果,並在 `str` 字串中使用 `replacement` 字串取代。:若有 `pattern:g` 旗標,取代全部匹配結果,反之只取代第一個匹配。
170+
- `regexp.test(str)` 方法尋找至少一個匹配結果,如果有找到,傳回 `true`,反之則是 `false`

0 commit comments

Comments
 (0)