You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In both cases `regexp`becomes an instance of the built-in `RegExp`class.
28
+
兩種請況下的 `regexp`都會變成內建 `RegExp`類別的實體。
29
29
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.
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:
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:
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 (來源字串)
100
94
```
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>.
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.:
let matches = "JavaScript".match(/HTML/); // = null
@@ -112,50 +105,50 @@ It has 3 working modes:
112
105
}
113
106
```
114
107
115
-
If we'd like the result to always be an array, we can write it this way:
108
+
如果我們想要結果永遠是一個陣列,我們可以這麼寫:
116
109
117
110
```js run
118
111
let matches = "JavaScript".match(/HTML/)*!* || []*/!*;
119
112
120
113
if (!matches.length) {
121
-
alert("No matches"); // now it works
114
+
alert("No matches"); // 現在會動了
122
115
}
123
116
```
124
117
125
-
## Replacing:str.replace
118
+
## 取代:str.replace
126
119
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).
Later in this chapter we'll study more regular expressions, walk through more examples, and also meet other methods.
160
+
之後的章節我們將學習更多正規表達式,經過更多的範例,了解其他方法。
168
161
169
-
Full information about the methods is given in the article <info:regexp-methods>.
162
+
關於方法的完整資訊在此文章 <info:regexp-methods> 內提供。
170
163
171
-
## Summary
164
+
## 總結
172
165
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`.
0 commit comments