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
Copy file name to clipboardExpand all lines: 9-regular-expressions/01-regexp-introduction/article.md
+17-17
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,22 @@
1
1
# Patterns and flags
2
2
3
-
Regular expressions is a powerful way to search and replace in text.
3
+
Regular expressions are patterns that provide a powerful way to search and replace in text.
4
4
5
-
In JavaScript, they are available as [RegExp](mdn:js/RegExp) object, and also integrated in methods of strings.
5
+
In JavaScript, they are available via the [RegExp](mdn:js/RegExp) object, as well as being integrated in methods of strings.
6
6
7
7
## Regular Expressions
8
8
9
9
A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*.
10
10
11
-
There are two syntaxes to create a regular expression object.
11
+
There are two syntaxes that can be used to create a regular expression object.
12
12
13
13
The "long" syntax:
14
14
15
15
```js
16
16
regexp =newRegExp("pattern", "flags");
17
17
```
18
18
19
-
...And the short one, using slashes `"/"`:
19
+
And the "short" one, using slashes `"/"`:
20
20
21
21
```js
22
22
regexp =/pattern/; // no flags
@@ -25,11 +25,11 @@ regexp = /pattern/gmi; // with flags g,m and i (to be covered soon)
25
25
26
26
Slashes `pattern:/.../` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings.
27
27
28
-
In both cases `regexp` becomes an object of the built-in `RegExp` class.
28
+
In both cases `regexp` becomes an instance of the built-in `RegExp` class.
29
29
30
-
The main difference between these two syntaxes is that slashes `pattern:/.../`do not allow to insert expressions (like strings with `${...}`). They are fully static.
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.
31
31
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 used when we need to create a regexp "on the fly", from a dynamically generated string, for instance:
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:
33
33
34
34
```js
35
35
let tag =prompt("What tag do you want to find?", "h2");
@@ -47,7 +47,7 @@ There are only 6 of them in JavaScript:
47
47
: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below).
48
48
49
49
`pattern:g`
50
-
: With this flag the search looks for all matches, without it -- only the first one.
50
+
: With this flag the search looks for all matches, without it -- only the first match is returned.
51
51
52
52
`pattern:m`
53
53
: Multiline mode (covered in the chapter <info:regexp-multiline-mode>).
@@ -71,7 +71,7 @@ From here on the color scheme is:
71
71
72
72
## Searching: str.match
73
73
74
-
As it was said previously, regular expressions are integrated with string methods.
74
+
As mentioned previously, regular expressions are integrated with string methods.
75
75
76
76
The method `str.match(regexp)` finds all matches of `regexp` in the string `str`.
77
77
@@ -102,7 +102,7 @@ It has 3 working modes:
102
102
103
103
3. And, finally, if there are no matches, `null` is returned (doesn't matter if there's flag `pattern:g` or not).
104
104
105
-
That's a very important nuance. If there are no matches, we get not an empty array, but `null`. Forgetting about that may lead to errors, e.g.:
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.:
106
106
107
107
```js run
108
108
let matches = "JavaScript".match(/HTML/); // = null
@@ -112,7 +112,7 @@ It has 3 working modes:
112
112
}
113
113
```
114
114
115
-
If we'd like the result to be always an array, we can write it this way:
115
+
If we'd like the result to always be an array, we can write it this way:
116
116
117
117
```js run
118
118
let matches = "JavaScript".match(/HTML/)*!* || []*/!*;
@@ -124,7 +124,7 @@ It has 3 working modes:
124
124
125
125
## Replacing:str.replace
126
126
127
-
The method `str.replace(regexp, replacement)` replaces matches with`regexp`in string `str`with`replacement` (all matches,if there's flag `pattern:g`, otherwise only the first one).
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).
128
128
129
129
For instance:
130
130
@@ -164,14 +164,14 @@ let regexp = /LOVE/i;
164
164
alert( regexp.test(str) ); // true
165
165
```
166
166
167
-
Further in this chapter we'll study more regular expressions, come across many other examples and also meet other methods.
167
+
Later in this chapter we'll study more regular expressions, walk through more examples, and also meet other methods.
168
168
169
169
Full information about the methods is given in the article <info:regexp-methods>.
170
170
171
171
## Summary
172
172
173
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 with `regexp`by`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 `false`.
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