Skip to content

Commit 5f540e5

Browse files
authored
Update 01-regexp-introduction
This PR just includes grammar and formatting suggestions.
1 parent 7ca5f53 commit 5f540e5

File tree

1 file changed

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

1 file changed

+17
-17
lines changed

9-regular-expressions/01-regexp-introduction/article.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# Patterns and flags
22

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.
44

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.
66

77
## Regular Expressions
88

99
A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*.
1010

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.
1212

1313
The "long" syntax:
1414

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

19-
...And the short one, using slashes `"/"`:
19+
And the "short" one, using slashes `"/"`:
2020

2121
```js
2222
regexp = /pattern/; // no flags
@@ -25,11 +25,11 @@ regexp = /pattern/gmi; // with flags g,m and i (to be covered soon)
2525

2626
Slashes `pattern:/.../` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings.
2727

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.
2929

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.
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 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:
3333

3434
```js
3535
let tag = prompt("What tag do you want to find?", "h2");
@@ -47,7 +47,7 @@ There are only 6 of them in JavaScript:
4747
: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below).
4848

4949
`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.
5151

5252
`pattern:m`
5353
: Multiline mode (covered in the chapter <info:regexp-multiline-mode>).
@@ -71,7 +71,7 @@ From here on the color scheme is:
7171

7272
## Searching: str.match
7373

74-
As it was said previously, regular expressions are integrated with string methods.
74+
As mentioned previously, regular expressions are integrated with string methods.
7575

7676
The method `str.match(regexp)` finds all matches of `regexp` in the string `str`.
7777

@@ -102,7 +102,7 @@ It has 3 working modes:
102102

103103
3. And, finally, if there are no matches, `null` is returned (doesn't matter if there's flag `pattern:g` or not).
104104

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.:
106106
107107
```js run
108108
let matches = "JavaScript".match(/HTML/); // = null
@@ -112,7 +112,7 @@ It has 3 working modes:
112112
}
113113
```
114114
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:
116116

117117
```js run
118118
let matches = "JavaScript".match(/HTML/)*!* || []*/!*;
@@ -124,7 +124,7 @@ It has 3 working modes:
124124

125125
## Replacing: str.replace
126126

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).
128128
129129
For instance:
130130
@@ -164,14 +164,14 @@ let regexp = /LOVE/i;
164164
alert( regexp.test(str) ); // true
165165
```
166166
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.
168168
169169
Full information about the methods is given in the article <info:regexp-methods>.
170170
171171
## Summary
172172
173173
- 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

Comments
 (0)